generated from Over-Run/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
310 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...lient/src/main/java/io/github/xenfork/freeworld/client/render/world/ChunkCompileTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* freeworld - 3D sandbox game | ||
* Copyright (C) 2024 XenFork Union | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
*/ | ||
|
||
package io.github.xenfork.freeworld.client.render.world; | ||
|
||
import io.github.xenfork.freeworld.client.render.GameRenderer; | ||
import io.github.xenfork.freeworld.client.render.builder.DefaultVertexBuilder; | ||
import io.github.xenfork.freeworld.util.Direction; | ||
import io.github.xenfork.freeworld.world.chunk.Chunk; | ||
import io.github.xenfork.freeworld.world.chunk.ChunkPos; | ||
|
||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.ValueLayout; | ||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public final class ChunkCompileTask implements Callable<ChunkVertexData> { | ||
private final GameRenderer gameRenderer; | ||
private final WorldRenderer worldRenderer; | ||
private final Chunk chunk; | ||
|
||
public ChunkCompileTask(GameRenderer gameRenderer, WorldRenderer worldRenderer, Chunk chunk) { | ||
this.gameRenderer = gameRenderer; | ||
this.worldRenderer = worldRenderer; | ||
this.chunk = chunk; | ||
} | ||
|
||
@Override | ||
public ChunkVertexData call() { | ||
final var pool = worldRenderer.vertexBuilderPool(); | ||
final DefaultVertexBuilder builder = pool.acquire(); | ||
try { | ||
final int cx = chunk.x(); | ||
final int cy = chunk.y(); | ||
final int cz = chunk.z(); | ||
for (Direction direction : Direction.LIST) { | ||
for (int x = 0; x < Chunk.SIZE; x++) { | ||
for (int y = 0; y < Chunk.SIZE; y++) { | ||
for (int z = 0; z < Chunk.SIZE; z++) { | ||
final int nx = x + direction.axisX(); | ||
final int ny = y + direction.axisY(); | ||
final int nz = z + direction.axisZ(); | ||
if (chunk.isInBound(nx, ny, nz) && chunk.getBlockType(nx, ny, nz).air()) { | ||
gameRenderer.blockRenderer().renderBlockFace( | ||
builder, | ||
chunk.getBlockType(x, y, z), | ||
ChunkPos.relativeToAbsolute(cx, x), | ||
ChunkPos.relativeToAbsolute(cy, y), | ||
ChunkPos.relativeToAbsolute(cz, z), | ||
direction); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
final Arena arena = Arena.ofShared(); | ||
final MemorySegment vertexDataSlice = builder.vertexDataSlice(); | ||
final MemorySegment indexDataSlice = builder.indexDataSlice(); | ||
return new ChunkVertexData( | ||
builder.vertexLayout(), | ||
builder.indexCount(), | ||
arena, | ||
arena.allocateFrom(ValueLayout.JAVA_BYTE, vertexDataSlice, ValueLayout.JAVA_BYTE, 0L, vertexDataSlice.byteSize()), | ||
arena.allocateFrom(ValueLayout.JAVA_BYTE, indexDataSlice, ValueLayout.JAVA_BYTE, 0L, indexDataSlice.byteSize()), | ||
builder.shouldReallocateVertexData(), | ||
builder.shouldReallocateIndexData() | ||
); | ||
} finally { | ||
pool.release(builder); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...client/src/main/java/io/github/xenfork/freeworld/client/render/world/ChunkVertexData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* freeworld - 3D sandbox game | ||
* Copyright (C) 2024 XenFork Union | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
*/ | ||
|
||
package io.github.xenfork.freeworld.client.render.world; | ||
|
||
import io.github.xenfork.freeworld.client.render.model.VertexLayout; | ||
|
||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemorySegment; | ||
|
||
/** | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public record ChunkVertexData( | ||
VertexLayout vertexLayout, | ||
int indexCount, | ||
Arena arena, | ||
MemorySegment vertexData, | ||
MemorySegment indexData, | ||
boolean shouldReallocateVertexData, | ||
boolean shouldReallocateIndexData | ||
) { | ||
} |
67 changes: 67 additions & 0 deletions
67
...ient/src/main/java/io/github/xenfork/freeworld/client/render/world/VertexBuilderPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* freeworld - 3D sandbox game | ||
* Copyright (C) 2024 XenFork Union | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
*/ | ||
|
||
package io.github.xenfork.freeworld.client.render.world; | ||
|
||
import io.github.xenfork.freeworld.client.render.builder.VertexBuilder; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public final class VertexBuilderPool<T extends VertexBuilder> { | ||
private final Map<Integer, Pair<T>> map = new ConcurrentHashMap<>(); | ||
private final Supplier<T> factory; | ||
|
||
public VertexBuilderPool(Supplier<T> factory) { | ||
this.factory = factory; | ||
} | ||
|
||
private static final class Pair<T extends VertexBuilder> { | ||
final T builder; | ||
final AtomicBoolean acquired; | ||
|
||
Pair(T builder, boolean acquired) { | ||
this.builder = builder; | ||
this.acquired = new AtomicBoolean(acquired); | ||
} | ||
} | ||
|
||
public T acquire() { | ||
for (var entry : map.entrySet()) { | ||
final Pair<T> value = entry.getValue(); | ||
if (!value.acquired.get()) { | ||
value.acquired.set(true); | ||
return value.builder; | ||
} | ||
} | ||
final T t = factory.get(); | ||
final int hashCode = System.identityHashCode(t); | ||
map.put(hashCode, new Pair<>(t, true)); | ||
return t; | ||
} | ||
|
||
public void release(T t) { | ||
Objects.requireNonNull(t); | ||
final int hashCode = System.identityHashCode(t); | ||
if (map.containsKey(hashCode)) { | ||
final Pair<T> pair = map.get(hashCode); | ||
if (pair.acquired.get()) { | ||
pair.acquired.set(false); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.