Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
34 changes: 8 additions & 26 deletions src/main/java/dev/dubhe/anvilcraft/api/power/SimplePowerGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.client.renderer.Line;
import dev.dubhe.anvilcraft.util.Line;
import dev.dubhe.anvilcraft.client.support.PowerGridSupport;
import dev.dubhe.anvilcraft.util.ColorUtil;
import dev.dubhe.anvilcraft.util.ShapeUtil;
import dev.dubhe.anvilcraft.util.VirtualThreadFactoryImpl;
import dev.dubhe.anvilcraft.util.algo.MinimumSpanningTree3D;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -217,33 +218,14 @@ public boolean shouldRender(Vec3 cameraPos) {
}

private void createTransmitterVisualLines() {
List<Map.Entry<BlockPos, AABB>> shapes = this.powerComponentInfoList.stream()
.filter(it -> it.type() == PowerComponentType.TRANSMITTER)
.map(it -> Map.entry(
it.pos(), new AABB(
-it.range() + it.pos().getX(),
-it.range() + it.pos().getY(),
-it.range() + it.pos().getZ(),
it.range() + 1 + it.pos().getX(),
it.range() + 1 + it.pos().getY(),
it.range() + 1 + it.pos().getZ()
)
))
.toList();

for (int i = 0; i < shapes.size(); i++) {
Map.Entry<BlockPos, AABB> e1 = shapes.get(i);
for (int j = i + 1; j < shapes.size(); j++) {
Map.Entry<BlockPos, AABB> e2 = shapes.get(j);
AABB a = e1.getValue();
AABB b = e2.getValue();
if (a.intersects(b)) {
Vec3 start = e1.getKey().getCenter();
Vec3 end = e2.getKey().getCenter();
powerTransmitterLines.add(new Line(start, end));
}
List<Vec3> list = new ArrayList<>();
for (PowerComponentInfo powerComponentInfo : this.powerComponentInfoList) {
if (powerComponentInfo.type() == PowerComponentType.TRANSMITTER) {
list.add(powerComponentInfo.pos().getCenter());
}
}
List<Line> lines = MinimumSpanningTree3D.kruskalMST(list);
powerTransmitterLines.addAll(lines);
}

private void createMergedOutlineShape() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.dubhe.anvilcraft.api.rendering;
package dev.dubhe.anvilcraft.api.rendering.foundation;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.VertexBuffer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.dubhe.anvilcraft.api.rendering.foundation;

public interface Disposable {
/**
* It is guaranteed to run on Render Thread.
*/
void dispose();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.dubhe.anvilcraft.api.rendering;
package dev.dubhe.anvilcraft.api.rendering.foundation;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.ByteBufferBuilder;
Expand All @@ -7,6 +7,7 @@
import com.mojang.blaze3d.vertex.VertexConsumer;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
import lombok.Getter;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
Expand All @@ -24,7 +25,8 @@ public class FullyBufferedBufferSource extends MultiBufferSource.BufferSource im
private static final MemoryUtil.MemoryAllocator ALLOCATOR = MemoryUtil.getAllocator(false);
private final Map<RenderType, ByteBufferBuilder> byteBuffers = new HashMap<>();
private final Map<RenderType, BufferBuilder> bufferBuilders = new HashMap<>();
final Reference2IntMap<RenderType> indexCountMap = new Reference2IntOpenHashMap<>();
@Getter
private final Reference2IntMap<RenderType> indexCountMap = new Reference2IntOpenHashMap<>();

public FullyBufferedBufferSource() {
super(null, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.dubhe.anvilcraft.api.rendering.foundation;

import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.QuadSortingState;
import it.unimi.dsi.fastutil.ints.IntArrays;
import org.joml.Vector3f;

public class QuadSorter {
public static int[] buildSortedIndexByDistance(QuadSortingState state, Vector3f point){

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck> reported by reviewdog 🐶
WhitespaceAround: '{' is not preceded with whitespace.

float[] distances = new float[state.quadCenters().size()];
int[] indexes = new int[state.quadCenters().size()];

for (int i = 0; i < state.quadCenters().size(); i++) {
distances[i] = state.quadCenters().get(i).distanceSquared(point);
indexes[i] = i;
}
IntArrays.mergeSort(indexes, (a,b) -> Float.compare(distances[a], distances[b]));

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck> reported by reviewdog 🐶
',' is not followed by whitespace.

return indexes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer;

import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable;
import org.lwjgl.opengl.GL;

import static org.lwjgl.opengl.GL45.*;

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck> reported by reviewdog 🐶
Using the '.' form of import should be avoided - org.lwjgl.opengl.GL45..


public abstract class GlBufferStorage implements Disposable {
public static final boolean BUFFER_STORAGE_SUPPORT = GL.getCapabilities().GL_ARB_buffer_storage;
protected final int glBufferId;
protected final int target;
protected boolean valid = true;

GlBufferStorage(int target) {
this.target = target;
this.glBufferId = glGenBuffers();
}

public abstract void setupBufferState();

/**
* Runs on worker thread
*/
public abstract void upload(long ptr, long size);

public void bind() {
glBindBuffer(target, glBufferId);
}

@Override
public void dispose() {
if (!valid) return;
bind();
glDeleteBuffers(glBufferId);
unbind();
valid = false;
}

public void unbind() {
glBindBuffer(target, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer;

import com.mojang.blaze3d.systems.RenderSystem;

import static org.lwjgl.opengl.GL45.*;

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck> reported by reviewdog 🐶
Using the '.' form of import should be avoided - org.lwjgl.opengl.GL45..


public abstract class GlBufferStorageLegacy extends GlBufferStorage{

protected GlBufferStorageLegacy(int target) {
super(target);
bind();
this.setupBufferState();
unbind();
}

@Override
public void upload(long ptr, long size) {
RenderSystem.recordRenderCall(() ->
nglBufferData(this.target, size, ptr, GL_STATIC_DRAW)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer;

import net.minecraft.client.renderer.RenderType;
import org.lwjgl.opengl.ARBBufferStorage;
import org.lwjgl.system.MemoryUtil;

import static org.lwjgl.opengl.GL45.*;

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck> reported by reviewdog 🐶
Using the '.' form of import should be avoided - org.lwjgl.opengl.GL45..


public abstract class GlBufferStorageModern extends GlBufferStorage {
public static final long BUFFER_SIZE = RenderType.SMALL_BUFFER_SIZE;
public static final int FLAGS = ARBBufferStorage.GL_MAP_PERSISTENT_BIT
| ARBBufferStorage.GL_MAP_COHERENT_BIT
| GL_MAP_WRITE_BIT
| GL_DYNAMIC_STORAGE_BIT;

private long clientPtr;

protected GlBufferStorageModern(int target) {
super(target);
bind();
this.setupBufferState();
ARBBufferStorage.glBufferStorage(
target,
BUFFER_SIZE,
FLAGS
);
clientPtr = nglMapBufferRange(target, 0, BUFFER_SIZE, FLAGS);
unbind();
}

@Override
public void dispose() {
if (!valid)return;

Choose a reason for hiding this comment

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

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck> reported by reviewdog 🐶
WhitespaceAround: 'return' is not preceded with whitespace.

bind();
glUnmapBuffer(target);
clientPtr = 0;
super.dispose();
}

@Override
public void upload(long ptr, long size) {
MemoryUtil.memCopy(ptr, clientPtr, size);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex;

import com.mojang.blaze3d.vertex.VertexFormat;
import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable;
import dev.dubhe.anvilcraft.api.rendering.foundation.QuadSorter;
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorage;
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.index.GlIndexBuffer;
import net.minecraft.client.renderer.RenderType;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

import static org.lwjgl.opengl.GL45.*;

public class GlVertexArray implements Disposable {
private final int arrayObjectId;
private final GlBufferStorage vertexBuffer;
private final GlIndexBuffer indexBuffer;
private final RenderType renderType;
private final VertexFormat.IndexType indexType;
private QuadSortingState sortingState;

public GlVertexArray(RenderType renderType) {
this(renderType, VertexFormat.IndexType.SHORT);
}

public GlVertexArray(RenderType renderType, VertexFormat.IndexType indexType) {
this.indexType = indexType;
if (renderType.mode != VertexFormat.Mode.QUADS) throw new UnsupportedOperationException();
this.indexBuffer = GlIndexBuffer.forQuad(indexType);
this.renderType = renderType;
this.arrayObjectId = glGenVertexArrays();
glBindVertexArray(arrayObjectId);
this.vertexBuffer = GlVertexBufferStorage.create(renderType.format);
glBindVertexArray(0);
}

public void upload(long ptr, int size, int indexCount) {
this.upload(ptr, size, indexCount, null, null);
}

public void upload(long ptr, int size, int indexCount, @Nullable QuadSortingState sortingState, @Nullable Vector3f origin) {
this.sortingState = sortingState;
if (sortingState == null) {
indexBuffer.fillContents(indexCount);
} else {
resortVerticles(origin);
}
this.vertexBuffer.upload(ptr, size);
}

public void resortVerticles(Vector3f origin) {
int[] indexes = QuadSorter.buildSortedIndexByDistance(sortingState, origin);
indexBuffer.fromSorted(indexes);
}

@Override
public void dispose() {
vertexBuffer.dispose();
indexBuffer.dispose();
glDeleteVertexArrays(arrayObjectId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex;

import com.mojang.blaze3d.platform.GlConst;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorage;
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorageLegacy;
import dev.dubhe.anvilcraft.api.rendering.foundation.buffer.GlBufferStorageModern;

public final class GlVertexBufferStorage {

public static GlBufferStorage create(VertexFormat format) {
if (GlBufferStorage.BUFFER_STORAGE_SUPPORT) {
return new Modern(format);
}
return new Legacy(format);
}

public static class Legacy extends GlBufferStorageLegacy {
private final VertexFormat format;

public Legacy(VertexFormat format) {
super(GlConst.GL_ARRAY_BUFFER);
this.format = format;
}

@Override
public void setupBufferState() {
format.setupBufferState();
}
}

public static class Modern extends GlBufferStorageModern {
private final VertexFormat format;

public Modern(VertexFormat format) {
super(GlConst.GL_ARRAY_BUFFER);
this.format = format;
}

@Override
public void setupBufferState() {
format.setupBufferState();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex;

import org.joml.Vector3f;

import java.util.List;

public record QuadSortingState(List<Vector3f> quadCenters) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.dubhe.anvilcraft.api.rendering.foundation.buffer.vertex.index;

import com.mojang.blaze3d.vertex.VertexFormat;
import dev.dubhe.anvilcraft.api.rendering.foundation.Disposable;

public interface GlIndexBuffer extends Disposable {
/**
* Called on worker threads
*/
void fillContents(int indexCount);

/**
* Called on worker threads=
*/
void fromSorted(int[] sortedIndex);

void bind();

void unbind();

static GlIndexBuffer forQuad(VertexFormat.IndexType indexType){
return new GlQuadIndexBuffer(indexType);
}
}
Loading
Loading