mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Draw the 3D world on the discrete GPU, off the main thread
The studio drew on the integrated GPU of a hybrid laptop, where a view of the near ground block by block ran at a few frames a second. On Linux it now starts again, once, with NVIDIA's EGL driver and PRIME offload, its window's buffers made on the NVIDIA render node, unless the environment already names an EGL driver. Tiles are built on worker threads and sent back packed small: positions in sixteenths of a block, colours a byte a channel. A frame hands the GPU at most 16 MB of new meshes, and a tile keeps its old mesh until the finer ones filling its holes are drawn. The ground is drawn block by block until a block is a quarter of a pixel, with up to 256 tiles in sight. Runs now record the plants on their tops, which tint them as the near ground's plants do, and faces of neighbouring columns in one plane over the same heights merge.
This commit is contained in:
1 parent
428f9cfb0e
commit
ebd31db06f
25 files changed
+829
-193
No files matched your search
@@ -41,7 +41,8 @@ import java.util.function.Function;
|
||||
* u8 voxelX[voxels] | padding to 4 | u8 voxelZ[voxels] | padding to 4 | i16 voxelY[voxels] | padding to 4
|
||||
* | i16 voxelBlock[voxels] | padding to 4 | u8 voxelFaces[voxels] | padding to 4, when the header counts voxels
|
||||
* i16 runCounts[grid²] | padding to 4 | i16 runBottoms[runs] | padding to 4 | i16 runTops[runs] | padding to 4
|
||||
* | i16 runBlocks[runs] | padding to 4, when the header counts runs
|
||||
* | i16 runBlocks[runs] | padding to 4 | i16 runCovers[runs] | padding to 4 | u8 runCoverShares[runs] | padding to 4,
|
||||
* when the header counts runs
|
||||
* </pre>
|
||||
* {@code raised} is the sum of the raised counts, which the header also holds, as it holds {@code voxels}, the count of
|
||||
* blocks seen from the open air, and {@code runs}, the sum of the run counts, each -1 for a tile without them.
|
||||
@@ -79,7 +80,7 @@ final class MapTileEncoding {
|
||||
+ (hasPickNoise ? cells * Float.BYTES : 0)
|
||||
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 3 + aligned(raised * Short.BYTES) * 2)
|
||||
+ (voxels == null ? 0 : aligned(voxelCount) * 3 + aligned(voxelCount * Short.BYTES) * 2)
|
||||
+ (runs == null ? 0 : aligned(cells * Short.BYTES) + aligned(runCount * Short.BYTES) * 3);
|
||||
+ (runs == null ? 0 : aligned(cells * Short.BYTES) + aligned(runCount * Short.BYTES) * 4 + aligned(runCount));
|
||||
ByteBuffer buffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(header.length).put(header);
|
||||
align(buffer);
|
||||
@@ -112,6 +113,8 @@ final class MapTileEncoding {
|
||||
putShorts(buffer, runs.bottoms());
|
||||
putShorts(buffer, runs.tops());
|
||||
putShorts(buffer, runs.blocks());
|
||||
putShorts(buffer, runs.covers());
|
||||
putBytes(buffer, runs.coverShares());
|
||||
}
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ package art.arcane.iris.core.studio.map;
|
||||
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.util.project.hunk.Hunk;
|
||||
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
|
||||
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -35,22 +36,21 @@ import java.util.function.UnaryOperator;
|
||||
* solid blocks, highest first, as Distant Horizons draws far terrain. A height of an area counts as solid where at least
|
||||
* a quarter of its blocks are, so a tree is a canopy on a trunk with air beneath, not a pillar down to the ground. The
|
||||
* runs go down to the area's ground and no further; the last of each area is that ground, solid all the way down.
|
||||
* Plants and water fill no run, as they are drawn their own way.
|
||||
* Plants and water fill no run: plants count toward the look of the run they stand on, and water is drawn its own way.
|
||||
*
|
||||
* @param counts how many runs each area has, the areas row by row from the north west
|
||||
* @param bottoms the lowest height of each run, area after area
|
||||
* @param tops the highest height of each run
|
||||
* @param blocks the key of the block most of each run's top shows
|
||||
* @param counts how many runs each area has, the areas row by row from the north west
|
||||
* @param bottoms the lowest height of each run, area after area
|
||||
* @param tops the highest height of each run
|
||||
* @param blocks the key of the block most of each run's top shows
|
||||
* @param covers the key of the plant most often standing on each run's top, or null where none does
|
||||
* @param coverShares how much of each run's top its plants stand on, 0 for none to {@link #WHOLE_SHARE} for all of it
|
||||
*/
|
||||
record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks) {
|
||||
record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares) {
|
||||
static final int MOST_LEVEL = 4;
|
||||
static final int WHOLE_SHARE = 255;
|
||||
private static final int ACROSS = 16;
|
||||
private static final int SOLID_SHARE_DIVISOR = 4;
|
||||
|
||||
int areasAcross() {
|
||||
return (int) Math.round(Math.sqrt(counts.length));
|
||||
}
|
||||
|
||||
/** Where the runs of the area at {@code index} start in {@link #bottoms}. */
|
||||
int firstRunOf(int index) {
|
||||
int first = 0;
|
||||
@@ -70,24 +70,23 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
|
||||
int areasAcross = ACROSS / areaBlocks;
|
||||
int fewestSolid = Math.max(1, areaBlocks * areaBlocks / SOLID_SHARE_DIVISOR);
|
||||
short[] counts = new short[areasAcross * areasAcross];
|
||||
ShortArrayList bottoms = new ShortArrayList();
|
||||
ShortArrayList tops = new ShortArrayList();
|
||||
List<String> blocks = new ArrayList<>();
|
||||
Runs runs = new Runs();
|
||||
for (int areaZ = 0; areaZ < areasAcross; areaZ++) {
|
||||
for (int areaX = 0; areaX < areasAcross; areaX++) {
|
||||
Area area = new Area(chunk, areaX * areaBlocks, areaZ * areaBlocks, areaBlocks, seenByPlayers);
|
||||
int groundY = area.lowestOf(terrainY);
|
||||
int found = area.addRuns(fewestSolid, groundY, bottoms, tops, blocks);
|
||||
counts[areaZ * areasAcross + areaX] = (short) found;
|
||||
int before = runs.found;
|
||||
area.addRuns(fewestSolid, area.lowestOf(terrainY), runs);
|
||||
counts[areaZ * areasAcross + areaX] = (short) (runs.found - before);
|
||||
}
|
||||
}
|
||||
return new ColumnRuns(counts, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new));
|
||||
return runs.toColumnRuns(counts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof ColumnRuns runs && Arrays.equals(counts, runs.counts) && Arrays.equals(bottoms, runs.bottoms)
|
||||
&& Arrays.equals(tops, runs.tops) && Arrays.equals(blocks, runs.blocks);
|
||||
&& Arrays.equals(tops, runs.tops) && Arrays.equals(blocks, runs.blocks) && Arrays.equals(covers, runs.covers)
|
||||
&& Arrays.equals(coverShares, runs.coverShares);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,57 +96,75 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ColumnRuns" + Arrays.toString(counts) + Arrays.toString(bottoms) + Arrays.toString(tops) + Arrays.toString(blocks);
|
||||
return "ColumnRuns" + Arrays.toString(counts) + Arrays.toString(bottoms) + Arrays.toString(tops) + Arrays.toString(blocks)
|
||||
+ Arrays.toString(covers) + Arrays.toString(coverShares);
|
||||
}
|
||||
|
||||
/** The runs of one area as they are found from the top down. */
|
||||
/** The plant most often standing on a run's top, and how much of the top plants stand on. */
|
||||
private record Cover(String block, int share) {
|
||||
static final Cover NONE = new Cover(null, 0);
|
||||
}
|
||||
|
||||
/** The runs of every area as they are found, area after area and each area's from the top down. */
|
||||
private static final class Runs {
|
||||
private static final int NONE = -1;
|
||||
private final ShortArrayList bottoms;
|
||||
private final ShortArrayList tops;
|
||||
private final List<String> blocks;
|
||||
private final ShortArrayList bottoms = new ShortArrayList();
|
||||
private final ShortArrayList tops = new ShortArrayList();
|
||||
private final List<String> blocks = new ArrayList<>();
|
||||
private final List<String> covers = new ArrayList<>();
|
||||
private final ByteArrayList coverShares = new ByteArrayList();
|
||||
private int top = NONE;
|
||||
private int bottom;
|
||||
private String block;
|
||||
private Cover cover;
|
||||
private int found;
|
||||
|
||||
Runs(ShortArrayList bottoms, ShortArrayList tops, List<String> blocks) {
|
||||
this.bottoms = bottoms;
|
||||
this.tops = tops;
|
||||
this.blocks = blocks;
|
||||
boolean isOpen() {
|
||||
return top != NONE;
|
||||
}
|
||||
|
||||
void extendTo(int y, String shown) {
|
||||
if (top != NONE && !shown.equals(block)) {
|
||||
end(bottom);
|
||||
}
|
||||
if (top == NONE) {
|
||||
top = y;
|
||||
block = shown;
|
||||
}
|
||||
boolean continues(String shown) {
|
||||
return isOpen() && shown.equals(block);
|
||||
}
|
||||
|
||||
void start(int y, String shown, Cover onTop) {
|
||||
top = y;
|
||||
bottom = y;
|
||||
block = shown;
|
||||
cover = onTop;
|
||||
}
|
||||
|
||||
void extendTo(int y) {
|
||||
bottom = y;
|
||||
}
|
||||
|
||||
void endAbove(int y) {
|
||||
if (top != NONE) {
|
||||
end(y + 1);
|
||||
void end() {
|
||||
if (isOpen()) {
|
||||
add(bottom);
|
||||
}
|
||||
}
|
||||
|
||||
// The ground reaches down out of sight, so its run has no bottom to draw.
|
||||
void endAsGround() {
|
||||
if (top != NONE) {
|
||||
end(0);
|
||||
if (isOpen()) {
|
||||
add(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void end(int runBottom) {
|
||||
private void add(int runBottom) {
|
||||
bottoms.add((short) runBottom);
|
||||
tops.add((short) top);
|
||||
blocks.add(block);
|
||||
covers.add(cover.block());
|
||||
coverShares.add((byte) cover.share());
|
||||
found++;
|
||||
top = NONE;
|
||||
}
|
||||
|
||||
ColumnRuns toColumnRuns(short[] counts) {
|
||||
return new ColumnRuns(counts, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new),
|
||||
covers.toArray(String[]::new), coverShares.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
/** One square area of a chunk, read a height at a time. */
|
||||
@@ -164,21 +181,25 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
|
||||
|
||||
// From the top down, a run holds the solid heights in a row that mostly show one block, so a canopy, the trunk
|
||||
// under it and the ground are runs of their own. Once a run reaches the ground, it is the ground and the last.
|
||||
int addRuns(int fewestSolid, int groundY, ShortArrayList bottoms, ShortArrayList tops, List<String> blocks) {
|
||||
Runs runs = new Runs(bottoms, tops, blocks);
|
||||
void addRuns(int fewestSolid, int groundY, Runs runs) {
|
||||
for (int y = chunk.getHeight() - 1; y >= 0; y--) {
|
||||
if (solidCountAt(y) < fewestSolid) {
|
||||
runs.endAbove(y);
|
||||
runs.end();
|
||||
continue;
|
||||
}
|
||||
runs.extendTo(y, mostShownAt(y));
|
||||
String shown = mostShownAt(y);
|
||||
if (runs.continues(shown)) {
|
||||
runs.extendTo(y);
|
||||
} else {
|
||||
runs.end();
|
||||
runs.start(y, shown, coverOn(y));
|
||||
}
|
||||
if (y <= groundY) {
|
||||
runs.endAsGround();
|
||||
return runs.found;
|
||||
return;
|
||||
}
|
||||
}
|
||||
runs.endAsGround();
|
||||
return runs.found;
|
||||
}
|
||||
|
||||
private int solidCountAt(int y) {
|
||||
@@ -203,7 +224,30 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
|
||||
}
|
||||
}
|
||||
}
|
||||
return shown.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElseThrow();
|
||||
return mostOf(shown);
|
||||
}
|
||||
|
||||
// The plants standing on the area's blocks at the height, out of all the area's columns.
|
||||
private Cover coverOn(int y) {
|
||||
if (y + 1 >= chunk.getHeight()) {
|
||||
return Cover.NONE;
|
||||
}
|
||||
Map<String, Integer> plants = new HashMap<>();
|
||||
int standing = 0;
|
||||
for (int z = minZ; z < minZ + across; z++) {
|
||||
for (int x = minX; x < minX + across; x++) {
|
||||
PlatformBlockState above = chunk.get(x, y + 1, z);
|
||||
if (isSolid(chunk.get(x, y, z)) && isPlant(above)) {
|
||||
plants.merge(above.key(), 1, Integer::sum);
|
||||
standing++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return standing == 0 ? Cover.NONE : new Cover(mostOf(plants), standing * WHOLE_SHARE / (across * across));
|
||||
}
|
||||
|
||||
private static String mostOf(Map<String, Integer> counts) {
|
||||
return counts.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElseThrow();
|
||||
}
|
||||
|
||||
private boolean isSolid(PlatformBlockState block) {
|
||||
@@ -213,5 +257,13 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
|
||||
PlatformBlockState seen = seenByPlayers.apply(block);
|
||||
return !seen.isAir() && !seen.isFluid() && seen.isSolid();
|
||||
}
|
||||
|
||||
private boolean isPlant(PlatformBlockState block) {
|
||||
if (block == null || block.isAir()) {
|
||||
return false;
|
||||
}
|
||||
PlatformBlockState seen = seenByPlayers.apply(block);
|
||||
return !seen.isAir() && !seen.isFluid() && !seen.isSolid();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,8 +51,11 @@ interface ColumnTops {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** One area's runs of solid blocks, highest first, the last being the ground. */
|
||||
record AreaRuns(short[] bottoms, short[] tops, String[] blocks) {
|
||||
/**
|
||||
* One area's runs of solid blocks, highest first, the last being the ground, with the plant most often standing on
|
||||
* each run's top, or null, and how much of the top plants stand on, out of {@link ColumnRuns#WHOLE_SHARE}.
|
||||
*/
|
||||
record AreaRuns(short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -264,7 +264,8 @@ final class GeneratedChunks implements ColumnTops {
|
||||
int first = runs.firstRunOf(area);
|
||||
int last = first + runs.counts()[area];
|
||||
return new AreaRuns(Arrays.copyOfRange(runs.bottoms(), first, last), Arrays.copyOfRange(runs.tops(), first, last),
|
||||
Arrays.copyOfRange(runs.blocks(), first, last));
|
||||
Arrays.copyOfRange(runs.blocks(), first, last), Arrays.copyOfRange(runs.covers(), first, last),
|
||||
Arrays.copyOfRange(runs.coverShares(), first, last));
|
||||
}
|
||||
|
||||
private static void awaitUnlessCancelled(Future<?> chunk, BooleanSupplier cancelled) {
|
||||
|
||||
@@ -61,9 +61,11 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
|
||||
* @param counts how many runs each cell has
|
||||
* @param bottoms the lowest height of each run, cell after cell
|
||||
* @param tops the highest height of each run
|
||||
* @param blocks the index in the tile's block names of the block most of each run's top shows
|
||||
* @param blocks the index in the tile's block names of the block most of each run's top shows
|
||||
* @param covers the index of the plant most often standing on each run's top, or {@link Blocks#NO_BLOCK}
|
||||
* @param coverShares how much of each run's top plants stand on, 0 to 255 for all of it
|
||||
*/
|
||||
public record Runs(short[] counts, short[] bottoms, short[] tops, short[] blocks) {
|
||||
public record Runs(short[] counts, short[] bottoms, short[] tops, short[] blocks, short[] covers, byte[] coverShares) {
|
||||
public int count() {
|
||||
return tops.length;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import art.arcane.iris.engine.framework.render.RenderType;
|
||||
import art.arcane.iris.engine.object.world.biome.IrisBiome;
|
||||
import art.arcane.iris.engine.object.world.region.IrisRegion;
|
||||
import art.arcane.iris.util.project.stream.ProceduralStream;
|
||||
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
|
||||
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -130,6 +131,8 @@ public final class MapTileRenderer {
|
||||
ShortArrayList bottoms = new ShortArrayList();
|
||||
ShortArrayList runTops = new ShortArrayList();
|
||||
ShortArrayList blocks = new ShortArrayList();
|
||||
ShortArrayList covers = new ShortArrayList();
|
||||
ByteArrayList coverShares = new ByteArrayList();
|
||||
for (int gridZ = 0; gridZ < grid.size(); gridZ++) {
|
||||
int worldZ = (int) Math.floor(grid.worldZ(gridZ));
|
||||
for (int gridX = 0; gridX < grid.size(); gridX++) {
|
||||
@@ -140,12 +143,15 @@ public final class MapTileRenderer {
|
||||
counts[gridZ * grid.size() + gridX] = (short) area.tops().length;
|
||||
bottoms.addElements(bottoms.size(), area.bottoms());
|
||||
runTops.addElements(runTops.size(), area.tops());
|
||||
for (String block : area.blocks()) {
|
||||
blocks.add(names.indexOf(block));
|
||||
for (int run = 0; run < area.tops().length; run++) {
|
||||
blocks.add(names.indexOf(area.blocks()[run]));
|
||||
covers.add(names.indexOf(area.covers()[run]));
|
||||
}
|
||||
coverShares.addElements(coverShares.size(), area.coverShares());
|
||||
}
|
||||
}
|
||||
return new MapTile.Runs(counts, bottoms.toShortArray(), runTops.toShortArray(), blocks.toShortArray());
|
||||
return new MapTile.Runs(counts, bottoms.toShortArray(), runTops.toShortArray(), blocks.toShortArray(), covers.toShortArray(),
|
||||
coverShares.toByteArray());
|
||||
}
|
||||
|
||||
// Only a tile of a block a pixel shows its blocks one by one; coarser ones show the tops of their columns.
|
||||
|
||||
@@ -17,6 +17,7 @@ public class ColumnRunsTest {
|
||||
private static final PlatformBlockState GRASS = block("minecraft:grass_block");
|
||||
private static final PlatformBlockState LOG = block("minecraft:oak_log");
|
||||
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves");
|
||||
private static final PlatformBlockState GRASS_PLANT = plant("minecraft:short_grass");
|
||||
|
||||
@Test
|
||||
public void flatGroundIsOneRunPerArea() {
|
||||
@@ -67,6 +68,18 @@ public class ColumnRunsTest {
|
||||
assertEquals(GROUND_Y, runs.tops()[first + 1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aRunKnowsThePlantsStandingOnItsTop() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
chunk.set(0, GROUND_Y + 1, 0, GRASS_PLANT);
|
||||
|
||||
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
|
||||
|
||||
assertEquals("minecraft:short_grass", runs.covers()[0]);
|
||||
assertEquals(ColumnRuns.WHOLE_SHARE / 4, runs.coverShares()[0] & 0xFF);
|
||||
assertEquals(null, runs.covers()[1]);
|
||||
}
|
||||
|
||||
private static Hunk<PlatformBlockState> groundUpTo(int topY) {
|
||||
Hunk<PlatformBlockState> chunk = Hunk.newArrayHunk(16, HEIGHT, 16);
|
||||
for (int y = 0; y <= topY; y++) {
|
||||
@@ -85,4 +98,10 @@ public class ColumnRunsTest {
|
||||
when(block.isSolid()).thenReturn(true);
|
||||
return block;
|
||||
}
|
||||
|
||||
private static PlatformBlockState plant(String key) {
|
||||
PlatformBlockState block = mock(PlatformBlockState.class);
|
||||
when(block.key()).thenReturn(key);
|
||||
return block;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user