Skip to content

Commit

Permalink
Merge branch '1.20/dev' into feat/multi-loader-1.21
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/src/lib/java/dev/engine_room/flywheel/lib/model/part/ModelPartConverter.java
#	forge/src/main/java/dev/engine_room/flywheel/impl/ForgeFlwConfig.java
#	gradle.properties
  • Loading branch information
IThundxr committed Sep 20, 2024
2 parents 9365011 + 5b6463b commit e7e3579
Show file tree
Hide file tree
Showing 120 changed files with 2,846 additions and 1,025 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public interface InstanceHandle {
void setChanged();

void setDeleted();

void setVisible(boolean visible);

boolean isVisible();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.engine_room.flywheel.backend.compile.LightSmoothness;

public interface BackendConfig {
BackendConfig INSTANCE = FlwBackendXplat.INSTANCE.getConfig();
BackendConfig INSTANCE = FlwBackend.config();

/**
* How smooth/accurate our flw_light impl is.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package dev.engine_room.flywheel.backend;

import org.jetbrains.annotations.UnknownNullability;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dev.engine_room.flywheel.api.Flywheel;

public final class FlwBackend {
public static final Logger LOGGER = LoggerFactory.getLogger(Flywheel.ID + "/backend");
@UnknownNullability
private static BackendConfig config;

private FlwBackend() {
}

public static void init() {
public static BackendConfig config() {
return config;
}

public static void init(BackendConfig config) {
FlwBackend.config = config;
Backends.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ public interface FlwBackendXplat {
FlwBackendXplat INSTANCE = DependencyInjection.load(FlwBackendXplat.class, "dev.engine_room.flywheel.backend.FlwBackendXplatImpl");

int getLightEmission(BlockState state, BlockGetter level, BlockPos pos);

BackendConfig getConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class IndirectPrograms extends AtomicReferenceCounted {
private static final ResourceLocation CULL_SHADER_MAIN = Flywheel.rl("internal/indirect/cull.glsl");
private static final ResourceLocation APPLY_SHADER_MAIN = Flywheel.rl("internal/indirect/apply.glsl");
private static final ResourceLocation SCATTER_SHADER_MAIN = Flywheel.rl("internal/indirect/scatter.glsl");
private static final ResourceLocation DOWNSAMPLE_FIRST = Flywheel.rl("internal/indirect/downsample_first.glsl");
private static final ResourceLocation DOWNSAMPLE_SECOND = Flywheel.rl("internal/indirect/downsample_second.glsl");
public static final List<ResourceLocation> UTIL_SHADERS = List.of(APPLY_SHADER_MAIN, SCATTER_SHADER_MAIN, DOWNSAMPLE_FIRST, DOWNSAMPLE_SECOND);

private static final Compile<InstanceType<?>> CULL = new Compile<>();
private static final Compile<ResourceLocation> UTIL = new Compile<>();
Expand All @@ -42,14 +45,12 @@ public class IndirectPrograms extends AtomicReferenceCounted {

private final Map<PipelineProgramKey, GlProgram> pipeline;
private final Map<InstanceType<?>, GlProgram> culling;
private final GlProgram apply;
private final GlProgram scatter;
private final Map<ResourceLocation, GlProgram> utils;

private IndirectPrograms(Map<PipelineProgramKey, GlProgram> pipeline, Map<InstanceType<?>, GlProgram> culling, GlProgram apply, GlProgram scatter) {
private IndirectPrograms(Map<PipelineProgramKey, GlProgram> pipeline, Map<InstanceType<?>, GlProgram> culling, Map<ResourceLocation, GlProgram> utils) {
this.pipeline = pipeline;
this.culling = culling;
this.apply = apply;
this.scatter = scatter;
this.utils = utils;
}

private static List<String> getExtensions(GlslVersion glslVersion) {
Expand Down Expand Up @@ -94,10 +95,10 @@ static void reload(ShaderSources sources, ImmutableList<PipelineProgramKey> pipe
try {
var pipelineResult = pipelineCompiler.compileAndReportErrors(pipelineKeys);
var cullingResult = cullingCompiler.compileAndReportErrors(createCullingKeys());
var utils = utilCompiler.compileAndReportErrors(List.of(APPLY_SHADER_MAIN, SCATTER_SHADER_MAIN));
var utils = utilCompiler.compileAndReportErrors(UTIL_SHADERS);

if (pipelineResult != null && cullingResult != null && utils != null) {
newInstance = new IndirectPrograms(pipelineResult, cullingResult, utils.get(APPLY_SHADER_MAIN), utils.get(SCATTER_SHADER_MAIN));
newInstance = new IndirectPrograms(pipelineResult, cullingResult, utils);
}
} catch (Throwable t) {
FlwPrograms.LOGGER.error("Failed to compile indirect programs", t);
Expand Down Expand Up @@ -177,11 +178,19 @@ public GlProgram getCullingProgram(InstanceType<?> instanceType) {
}

public GlProgram getApplyProgram() {
return apply;
return utils.get(APPLY_SHADER_MAIN);
}

public GlProgram getScatterProgram() {
return scatter;
return utils.get(SCATTER_SHADER_MAIN);
}

public GlProgram getDownsampleFirstProgram() {
return utils.get(DOWNSAMPLE_FIRST);
}

public GlProgram getDownsampleSecondProgram() {
return utils.get(DOWNSAMPLE_SECOND);
}

@Override
Expand All @@ -190,6 +199,7 @@ protected void _delete() {
.forEach(GlProgram::delete);
culling.values()
.forEach(GlProgram::delete);
apply.delete();
utils.values()
.forEach(GlProgram::delete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ protected void generateUnpacking(GlslBuilder builder) {

fnBody.ret(GlslExpr.call(STRUCT_NAME, unpackArgs));

builder._addRaw("uniform usamplerBuffer _flw_instances;");
builder.uniform()
.type("usamplerBuffer")
.name("_flw_instances");
builder.blankLine();
builder.function()
.signature(FnSignature.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String source() {
var builder = new GlslBuilder();

var instance = builder.struct();
instance.setName(STRUCT_NAME);
instance.name(STRUCT_NAME);
for (var element : layout.elements()) {
instance.addField(LayoutInterpreter.typeName(element.type()), element.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void generateUnpacking(GlslBuilder builder) {

fnBody.ret(GlslExpr.call(STRUCT_NAME, unpackArgs));

builder._addRaw("layout(std430, binding = " + BufferBindings.INSTANCE + ") restrict readonly buffer InstanceBuffer {\n"
builder._raw("layout(std430, binding = " + BufferBindings.INSTANCE + ") restrict readonly buffer InstanceBuffer {\n"
+ " uint _flw_instances[];\n"
+ "};");
builder.blankLine();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package dev.engine_room.flywheel.backend.engine;

import dev.engine_room.flywheel.lib.memory.MemoryBlock;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;

public class Arena {
private final long elementSizeBytes;

private MemoryBlock memoryBlock;

// Monotonic index, generally represents the size of the arena.
private int top = 0;
public abstract class AbstractArena {
protected final long elementSizeBytes;
// List of free indices.
private final IntList freeStack = new IntArrayList();
// Monotonic index, generally represents the size of the arena.
private int top = 0;

public Arena(long elementSizeBytes, int initialCapacity) {
public AbstractArena(long elementSizeBytes) {
this.elementSizeBytes = elementSizeBytes;

memoryBlock = MemoryBlock.malloc(elementSizeBytes * initialCapacity);
}

public int alloc() {
Expand All @@ -27,8 +21,8 @@ public int alloc() {
}

// Make sure there's room to increment top.
if (top * elementSizeBytes >= memoryBlock.size()) {
memoryBlock = memoryBlock.realloc(memoryBlock.size() * 2);
if (top * elementSizeBytes >= byteCapacity()) {
grow();
}

// Return the top index and increment.
Expand All @@ -40,19 +34,15 @@ public void free(int i) {
freeStack.add(i);
}

public long indexToPointer(int i) {
return memoryBlock.ptr() + i * elementSizeBytes;
}

public void delete() {
memoryBlock.free();
public long byteOffsetOf(int i) {
return i * elementSizeBytes;
}

public int capacity() {
return top;
}

public long byteCapacity() {
return memoryBlock.size();
}
public abstract long byteCapacity();

protected abstract void grow();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.engine_room.flywheel.backend.engine;

import java.util.ArrayList;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

Expand All @@ -13,26 +14,32 @@
public abstract class AbstractInstancer<I extends Instance> implements Instancer<I> {
public final InstanceType<I> type;
public final Environment environment;
private final Supplier<AbstractInstancer<I>> recreate;

// Lock for all instances, only needs to be used in methods that may run on the TaskExecutor.
protected final Object lock = new Object();
protected final ArrayList<I> instances = new ArrayList<>();
protected final ArrayList<InstanceHandleImpl> handles = new ArrayList<>();
protected final ArrayList<InstanceHandleImpl<I>> handles = new ArrayList<>();

protected final AtomicBitSet changed = new AtomicBitSet();
protected final AtomicBitSet deleted = new AtomicBitSet();

protected AbstractInstancer(InstanceType<I> type, Environment environment) {
this.type = type;
this.environment = environment;
protected AbstractInstancer(InstancerKey<I> key, Supplier<AbstractInstancer<I>> recreate) {
this.type = key.type();
this.environment = key.environment();
this.recreate = recreate;
}

@Override
public I createInstance() {
synchronized (lock) {
var i = instances.size();
var handle = new InstanceHandleImpl(this, i);
var handle = new InstanceHandleImpl<I>();
handle.instancer = this;
handle.recreate = recreate;
handle.index = i;
I instance = type.create(handle);
handle.instance = instance;

addLocked(instance, handle);
return instance;
Expand All @@ -47,12 +54,15 @@ public void stealInstance(@Nullable I instance) {

var instanceHandle = instance.handle();

if (!(instanceHandle instanceof InstanceHandleImpl handle)) {
if (!(instanceHandle instanceof InstanceHandleImpl<?>)) {
// UB: do nothing
return;
}

if (handle.instancer == this) {
// Should InstanceType have an isInstance method?
var handle = (InstanceHandleImpl<I>) instanceHandle;

if (handle.instancer == this && handle.visible) {
return;
}

Expand All @@ -65,19 +75,23 @@ public void stealInstance(@Nullable I instance) {
// is filtering deleted instances later, so is safe.
handle.setDeleted();

// Only lock now that we'll be mutating our state.
synchronized (lock) {
// Add the instance to this instancer.
handle.instancer = this;
handle.index = instances.size();
addLocked(instance, handle);
// Add the instance to this instancer.
handle.instancer = this;
handle.recreate = recreate;

if (handle.visible) {
// Only lock now that we'll be mutating our state.
synchronized (lock) {
handle.index = instances.size();
addLocked(instance, handle);
}
}
}

/**
* Calls must be synchronized on {@link #lock}.
*/
private void addLocked(I instance, InstanceHandleImpl handle) {
private void addLocked(I instance, InstanceHandleImpl<I> handle) {
instances.add(instance);
handles.add(handle);
changed.set(handle.index);
Expand Down Expand Up @@ -122,7 +136,7 @@ protected void removeDeletedInstances() {

if (writePos < newSize) {
// Since we'll be shifting everything into this space we can consider it all changed.
changed.set(writePos, newSize);
setRangeChanged(writePos, newSize);
}

// We definitely shouldn't consider the deleted instances as changed though,
Expand Down Expand Up @@ -155,11 +169,15 @@ protected void removeDeletedInstances() {
.clear();
}

protected void setRangeChanged(int start, int end) {
changed.set(start, end);
}

/**
* Clear all instances without freeing resources.
*/
public void clear() {
for (InstanceHandleImpl handle : handles) {
for (InstanceHandleImpl<I> handle : handles) {
// Only clear instances that belong to this instancer.
// If one of these handles was stolen by another instancer,
// clearing it here would cause significant visual artifacts and instance leaks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.engine_room.flywheel.backend.engine;

import dev.engine_room.flywheel.lib.memory.MemoryBlock;

public class CpuArena extends AbstractArena {

private MemoryBlock memoryBlock;

public CpuArena(long elementSizeBytes, int initialCapacity) {
super(elementSizeBytes);

memoryBlock = MemoryBlock.malloc(elementSizeBytes * initialCapacity);
}

public long indexToPointer(int i) {
return memoryBlock.ptr() + i * elementSizeBytes;
}

public void delete() {
memoryBlock.free();
}

public long byteCapacity() {
return memoryBlock.size();
}

protected void grow() {
memoryBlock = memoryBlock.realloc(memoryBlock.size() * 2);
}
}
Loading

0 comments on commit e7e3579

Please sign in to comment.