From a09708bb309865e92c89afce7df7e730edd15915 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Mon, 20 Oct 2025 10:00:04 +1000 Subject: [PATCH] begin prep for offthread work --- .../core/model/ModelBakerySubsystem.java | 5 +- .../voxy/client/core/model/ModelFactory.java | 50 +++++++++++++------ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/main/java/me/cortex/voxy/client/core/model/ModelBakerySubsystem.java b/src/main/java/me/cortex/voxy/client/core/model/ModelBakerySubsystem.java index 261520f7..b6211dff 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/ModelBakerySubsystem.java +++ b/src/main/java/me/cortex/voxy/client/core/model/ModelBakerySubsystem.java @@ -66,8 +66,6 @@ public class ModelBakerySubsystem { }*/ //TimingStatistics.modelProcess.start(); if (this.blockIdCount.get() != 0) { - long budget = Math.min(totalBudget-150_000, totalBudget-(this.factory.resultJobs.size()*10_000L))-150_000; - //Always do 1 iteration minimum Integer i = this.blockIdQueue.poll(); int j = 0; @@ -90,8 +88,7 @@ public class ModelBakerySubsystem { this.factory.tick(); long start = System.nanoTime(); - while (!this.factory.resultJobs.isEmpty()) { - this.factory.resultJobs.poll().run(); + while (this.factory.processResult()) { if (totalBudget<(System.nanoTime()-start)) break; } diff --git a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java index 059f05df..ca87fc28 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java +++ b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java @@ -117,7 +117,7 @@ public class ModelFactory { private final ModelStore storage; private final RawDownloadStream downstream = new RawDownloadStream(8*1024*1024);//8mb downstream - public final Deque resultJobs = new ArrayDeque<>(); + private final Deque rawBakeResults = new ArrayDeque<>(); private Object2IntMap customBlockStateIdMapping; @@ -147,6 +147,17 @@ public class ModelFactory { this.customBlockStateIdMapping = mapping; } + private record RawBakeResult(int blockId, BlockState blockState, MemoryBuffer rawData) { + public RawBakeResult(int blockId, BlockState blockState) { + this(blockId, blockState, new MemoryBuffer(MODEL_TEXTURE_SIZE*MODEL_TEXTURE_SIZE*2*4*6)); + } + + public RawBakeResult cpyBuf(long ptr) { + this.rawData.cpyFrom(ptr); + return this; + } + } + public boolean addEntry(int blockId) { if (this.idMappings[blockId] != -1) { return false; @@ -179,32 +190,40 @@ public class ModelFactory { } } - int TOTAL_FACES_TEXTURE_SIZE = MODEL_TEXTURE_SIZE*MODEL_TEXTURE_SIZE*2*4*6;// since both depth and colour are packed together, 6 faces, 4 bytes per pixel - int allocation = this.downstream.download(TOTAL_FACES_TEXTURE_SIZE, ptr -> { - ColourDepthTextureData[] textureData = new ColourDepthTextureData[6]; - final int FACE_SIZE = MODEL_TEXTURE_SIZE*MODEL_TEXTURE_SIZE; + RawBakeResult result = new RawBakeResult(blockId, blockState); + int allocation = this.downstream.download(MODEL_TEXTURE_SIZE*MODEL_TEXTURE_SIZE*2*4*6, ptr -> this.rawBakeResults.add(result.cpyBuf(ptr))); + this.bakery.renderToStream(blockState, this.downstream.getBufferId(), allocation); + return true; + } + + public boolean processResult() { + if (this.rawBakeResults.isEmpty()) + return false; + var result = this.rawBakeResults.poll(); + ColourDepthTextureData[] textureData = new ColourDepthTextureData[6]; + {//Create texture data + long ptr = result.rawData.address; + final int FACE_SIZE = MODEL_TEXTURE_SIZE * MODEL_TEXTURE_SIZE; for (int face = 0; face < 6; face++) { - long faceDataPtr = ptr + (FACE_SIZE*4)*face*2; + long faceDataPtr = ptr + (FACE_SIZE * 4) * face * 2; int[] colour = new int[FACE_SIZE]; int[] depth = new int[FACE_SIZE]; //Copy out colour for (int i = 0; i < FACE_SIZE; i++) { //De-interpolate results - colour[i] = MemoryUtil.memGetInt(faceDataPtr+ (i*4*2)); - depth[i] = MemoryUtil.memGetInt(faceDataPtr+ (i*4*2)+4); + colour[i] = MemoryUtil.memGetInt(faceDataPtr + (i * 4 * 2)); + depth[i] = MemoryUtil.memGetInt(faceDataPtr + (i * 4 * 2) + 4); } - textureData[face] = new ColourDepthTextureData(colour, depth, MODEL_TEXTURE_SIZE, MODEL_TEXTURE_SIZE); } - this.resultJobs.add(()->processTextureBakeResult(blockId, blockState, textureData)); - }); - this.bakery.renderToStream(blockState, this.downstream.getBufferId(), allocation); - return true; + } + result.rawData.free(); + this.processTextureBakeResult(result.blockId, result.blockState, textureData); + return !this.rawBakeResults.isEmpty(); } - //This is private void processTextureBakeResult(int blockId, BlockState blockState, ColourDepthTextureData[] textureData) { if (this.idMappings[blockId] != -1) { @@ -759,6 +778,9 @@ public class ModelFactory { } public void free() { + while (!this.rawBakeResults.isEmpty()) { + this.rawBakeResults.poll().rawData.free(); + } this.downstream.free(); this.bakery.free(); }