Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rendering): migrate chunk mesh generation Flux #4786

Merged
merged 75 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
2723a05
refactor(rendering): rework chunk mesh generation with RXJava
pollend Jun 21, 2021
f0ac410
cleanup
pollend Jun 21, 2021
ca841b7
chore: replace with computation
pollend Jun 21, 2021
093921c
refactor(rendering): rework chunk mesh generation with RXJava Flowable
pollend Jun 23, 2021
7791254
update tessellator
pollend Jun 23, 2021
8016258
chore: cleanup
pollend Jun 25, 2021
dea7815
chore: cleanup
pollend Jun 26, 2021
9df6538
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Jun 27, 2021
a8e2d11
Merge branch 'develop' of github.com:MovingBlocks/Terasology into ref…
pollend Jun 27, 2021
5902bbd
chore: add info
pollend Jun 27, 2021
1456a7a
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' of git…
pollend Jun 27, 2021
9b04a71
chore: better handle processing chunks that are unloaded
pollend Jun 27, 2021
543a293
chore: replace with reactor
pollend Jul 3, 2021
2826532
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Jul 3, 2021
08cafdc
chore: batch process chunks
pollend Jul 4, 2021
0acab6b
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' of git…
pollend Jul 4, 2021
d088ace
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Jul 4, 2021
8c6e918
chore: minor cleanup
pollend Jul 4, 2021
3da70fc
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' of git…
pollend Jul 4, 2021
f53e6bc
chore: limit number of parallel rails
pollend Jul 4, 2021
587dc29
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Jul 5, 2021
8d4264f
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Aug 1, 2021
4a951a8
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Aug 7, 2021
0005fcd
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Aug 24, 2021
f2ba2d7
Merge branch 'develop' of github.com:MovingBlocks/Terasology into ref…
pollend Aug 28, 2021
b04e07e
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' of git…
pollend Aug 28, 2021
bad0581
Merge branch 'develop' of github.com:MovingBlocks/Terasology into ref…
pollend Aug 28, 2021
fef81c6
chore: remove duplicate reactor and scheulders
pollend Aug 29, 2021
cfc7d58
chore: update interfaces
pollend Aug 29, 2021
7cbdc32
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Sep 3, 2021
73085f8
chore: clean up RenderableWorldImpl
pollend Sep 3, 2021
a1e3ae1
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' of git…
pollend Sep 3, 2021
79f80f6
chore: remove ChunkTask
pollend Sep 3, 2021
262c677
Merge remote-tracking branch 'origin/develop' into refactor/migrate-c…
keturn Sep 5, 2021
bd783e6
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Sep 5, 2021
e15b975
chore: simplify consumer
pollend Sep 5, 2021
950acbe
handle emission error when submitting chunk for mesh
pollend Sep 6, 2021
e5069c7
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Oct 3, 2021
de2835d
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Nov 7, 2021
7072611
Merge branch 'develop' of github.com:MovingBlocks/Terasology into ref…
pollend Nov 20, 2021
60a63fe
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Nov 27, 2021
25e2983
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
pollend Dec 5, 2021
1bfcd3c
refactor: split out chunk work as a seperate helper class
pollend Dec 6, 2021
e7663be
chore: cleanup world renderer
pollend Dec 10, 2021
93fe4f8
Merge remote-tracking branch 'origin/develop' into refactor/migrate-c…
keturn Jan 19, 2022
4667639
fix(RenderableWorldImpl): update for ChunkMonitor change #4888
keturn Jan 19, 2022
90791df
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' into r…
keturn Jan 19, 2022
a15466b
chore(rendering.world): lint
keturn Jan 19, 2022
8e227a1
Merge pull request #4972 from MovingBlocks/refactor/split-chunk-proce…
keturn Jan 19, 2022
9562e86
test(ChunkMeshWorker): initial sketch of tests
keturn Jan 20, 2022
ff3b09c
refactor(Chunk): provide default implementations of methods where pos…
keturn Jan 20, 2022
130dfcd
refactor(ChunkMeshWorker): factor out the generateMesh method
keturn Jan 20, 2022
e032131
test(DummyChunk): stub implementation of Chunk
keturn Jan 20, 2022
e4c723a
test(ChunkMeshWorker): initial attempt at using reactor-test
keturn Jan 20, 2022
e9e32b7
test(ChunkMeshWorker): implement more tests
keturn Jan 22, 2022
cf27ab9
Merge remote-tracking branch 'origin/develop' into refactor/migrate-c…
keturn Jan 22, 2022
3f5a78f
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' into t…
keturn Jan 22, 2022
cf7710a
fix(ChunkMeshWorker): fix no-longer-processing test
keturn Jan 22, 2022
095e40d
refactor(ChunkMeshWorker): inline fluxNewMeshes
keturn Jan 22, 2022
72b0f67
refactor(Chunk): push dispose-old-mesh logic down in to Chunk.setMesh
keturn Jan 22, 2022
ca2f3be
refactor(ChunkMeshWorker): make worker function definitions more compact
keturn Feb 6, 2022
b41ebb1
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
keturn Feb 6, 2022
761a492
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
keturn Feb 18, 2022
430bafa
Merge remote-tracking branch 'origin/refactor/migrate-chunk-mesh-gene…
keturn Feb 18, 2022
972ee97
test(ChunkMeshWorker): remove some difficult to implement tests about…
keturn Feb 18, 2022
1f4ac45
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
jdrueckert Apr 3, 2022
6e534d7
Merge remote-tracking branch 'origin/develop' into refactor/migrate-c…
keturn May 3, 2022
4cc9ca1
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' into t…
keturn May 3, 2022
b313fb0
test(ChunkMeshWorker): make DummyChunk strings slightly more concise
keturn May 6, 2022
3db427f
test(ChunkMeshWorker): fix testMultipleChunks so the results are orde…
keturn May 6, 2022
d69eef8
test(ChunkMeshWorker): add some documentation
keturn May 7, 2022
a157225
Merge remote-tracking branch 'origin/develop' into refactor/migrate-c…
keturn May 7, 2022
f9c6475
Merge branch 'refactor/migrate-chunk-mesh-generation-flowable' into t…
keturn May 7, 2022
cfb307c
Merge pull request #4987 from MovingBlocks/test/chunkmeshworker
keturn May 7, 2022
4c296fe
Merge branch 'develop' into refactor/migrate-chunk-mesh-generation-fl…
keturn May 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
implementation "org.terasology:reflections:0.9.12-MB"
implementation group: 'org.javassist', name: 'javassist', version: '3.27.0-GA'
implementation group: 'com.esotericsoftware', name: 'reflectasm', version: '1.11.9'
implementation group: 'io.reactivex.rxjava3', name: 'rxjava', version: '3.0.13'

// Graphics, 3D, UI, etc
api platform("org.lwjgl:lwjgl-bom:$LwjglVersion")
Expand Down
19 changes: 19 additions & 0 deletions engine/src/main/java/org/terasology/engine/core/GameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;

/**
Expand All @@ -23,6 +27,21 @@ public final class GameThread {
private static volatile Thread gameThread;
private static BlockingDeque<Runnable> pendingRunnables = Queues.newLinkedBlockingDeque();

private static final Scheduler MAIN;

static {
MAIN = Schedulers.from(new Executor() {
@Override
public void execute(@NotNull Runnable runnable) {
pendingRunnables.add(runnable);
}
});
}

public static Scheduler main() {
return MAIN;
}

private GameThread() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ public enum RenderPhase {

private boolean disposed;

/* CONCURRENCY */
private ReentrantLock lock = new ReentrantLock();

/* MEASUREMENTS */
private int timeToGenerateBlockVertices;
private int timeToGenerateOptimizedBuffers;
Expand All @@ -95,46 +92,39 @@ public boolean hasVertexElements() {
* @return True if something was generated
*/
public boolean generateVBOs() {
if (lock.tryLock()) {
try {
// IMPORTANT: A mesh can only be generated once.
if (vertexElements == null || disposed) {
return false;
}

// Make sure that if it has already been generated, the previous buffers are freed
dispose();
disposed = false;

for (RenderType type : RenderType.values()) {
generateVBO(type);
}
// IMPORTANT: A mesh can only be generated once.
if (vertexElements == null || disposed) {
return false;
}

// Calculate the final amount of triangles
triangleCount = (vertexCount[0] + vertexCount[1] + vertexCount[2] + vertexCount[3]) / 3;
} finally {
lock.unlock();
}
// Make sure that if it has already been generated, the previous buffers are freed
dispose();
disposed = false;

return true;
for (RenderType type : RenderType.values()) {
generateVBO(type);
}

return false;
// Calculate the final amount of triangles
triangleCount = (vertexCount[0] + vertexCount[1] + vertexCount[2] + vertexCount[3]) / 3;

return true;
}

private void generateVBO(RenderType type) {
VertexElements elements = vertexElements[type.ordinal()];
int id = type.getIndex();
if (!disposed && elements.buffer.elements() > 0) {
vertexBuffers[id] = GL15.glGenBuffers();
idxBuffers[id] = GL15.glGenBuffers();
vertexBuffers[id] = GL30.glGenBuffers();
idxBuffers[id] = GL30.glGenBuffers();
vaoCount[id] = GL30.glGenVertexArrays();

GL30.glBindVertexArray(vaoCount[id]);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffers[id]);
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, vertexBuffers[id]);
elements.buffer.writeBuffer(buffer -> {
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL30.GL_STATIC_DRAW);
GL30.glBufferData(GL30.GL_ARRAY_BUFFER, buffer, GL30.GL_STATIC_DRAW);
});

for (VertexResource.VertexDefinition definition : elements.buffer.definitions()) {
Expand Down Expand Up @@ -171,19 +161,13 @@ public void discardData() {
}

private void renderVbo(int id) {
if (lock.tryLock()) {
try {
if (vertexBuffers[id] <= 0 || disposed) {
return;
}

GL30.glBindVertexArray(vaoCount[id]);
GL30.glDrawElements(GL30.GL_TRIANGLES, vertexCount[id], GL30.GL_UNSIGNED_INT, 0);
GL30.glBindVertexArray(0);
} finally {
lock.unlock();
}
if (vertexBuffers[id] <= 0 || disposed) {
return;
}

GL30.glBindVertexArray(vaoCount[id]);
GL30.glDrawElements(GL30.GL_TRIANGLES, vertexCount[id], GL30.GL_UNSIGNED_INT, 0);
GL30.glBindVertexArray(0);
}

/**
Expand Down Expand Up @@ -223,33 +207,28 @@ public int render(RenderPhase type) {
* ChunkMesh instances cannot be un-disposed.
*/
public void dispose() {
lock.lock();
try {
if (!disposed) {
for (int i = 0; i < vertexBuffers.length; i++) {
int id = vertexBuffers[i];
if (id != 0) {
GL15.glDeleteBuffers(id);
vertexBuffers[i] = 0;
}

id = idxBuffers[i];
if (id != 0) {
GL15.glDeleteBuffers(id);
idxBuffers[i] = 0;
}

id = vaoCount[i];
if (id != 0) {
GL30.glDeleteVertexArrays(id);
vaoCount[i] = 0;
}
if (!disposed) {
for (int i = 0; i < vertexBuffers.length; i++) {
int id = vertexBuffers[i];
if (id != 0) {
GL15.glDeleteBuffers(id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use GL15 but change other occurrences to GL30?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they end up mapping to the same call.

vertexBuffers[i] = 0;
}

disposed = true;
id = idxBuffers[i];
if (id != 0) {
GL15.glDeleteBuffers(id);
idxBuffers[i] = 0;
}

id = vaoCount[i];
if (id != 0) {
GL30.glDeleteVertexArrays(id);
vaoCount[i] = 0;
}
}
} finally {
lock.unlock();

disposed = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ public ChunkMesh generateMesh(ChunkView chunkView, float scale, int border) {

final Stopwatch watch = Stopwatch.createStarted();

watch.stop();
mesh.setTimeToGenerateBlockVertices((int) watch.elapsed(TimeUnit.MILLISECONDS));
watch.reset().start();

// The mesh extends into the borders in the horizontal directions, but not vertically upwards, in order to cover
// gaps between LOD chunks of different scales, but also avoid multiple overlapping ocean surfaces.
for (int x = 0; x < Chunks.SIZE_X; x++) {
for (int y = 0; y < Chunks.SIZE_Y - border * 2; y++) {
for (int z = 0; z < Chunks.SIZE_Z; z++) {
for (int y = 0; y < Chunks.SIZE_Y - border * 2; y++) {
for (int x = 0; x < Chunks.SIZE_X; x++) {
Block block = chunkView.getBlock(x, y, z);
if (block != null && block.getMeshGenerator() != null) {
block.getMeshGenerator().generateChunkMesh(chunkView, mesh, x, y, z);
Expand All @@ -63,7 +59,7 @@ public ChunkMesh generateMesh(ChunkView chunkView, float scale, int border) {
}

watch.stop();
mesh.setTimeToGenerateOptimizedBuffers((int) watch.elapsed(TimeUnit.MILLISECONDS));
mesh.setTimeToGenerateBlockVertices((int) watch.elapsed(TimeUnit.MILLISECONDS));
statVertexArrayUpdateCount++;

PerformanceMonitor.endActivity();
Expand Down
Loading