Skip to content

Commit

Permalink
Add RegisterRenderBuffersEvent to register fixed BufferBuilders for R…
Browse files Browse the repository at this point in the history
…enderTypes (#546)
  • Loading branch information
nikita488 authored Jan 21, 2024
1 parent 0c97f37 commit 5150301
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions patches/net/minecraft/client/renderer/RenderBuffers.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/net/minecraft/client/renderer/RenderBuffers.java
+++ b/net/minecraft/client/renderer/RenderBuffers.java
@@ -39,6 +_,7 @@
put(p_307112_, RenderType.waterMask());
ModelBakery.DESTROY_TYPES.forEach(p_173062_ -> put(p_307112_, p_173062_));
});
+ net.neoforged.fml.ModLoader.get().postEvent(new net.neoforged.neoforge.client.event.RegisterRenderBuffersEvent(sortedmap));
this.crumblingBufferSource = MultiBufferSource.immediate(new BufferBuilder(1536));
this.bufferSource = MultiBufferSource.immediateWithBuffers(sortedmap, new BufferBuilder(786432));
this.outlineBufferSource = new OutlineBufferSource(this.bufferSource);
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.event;

import com.mojang.blaze3d.vertex.BufferBuilder;
import java.util.SortedMap;
import net.minecraft.client.renderer.RenderType;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.event.IModBusEvent;
import org.jetbrains.annotations.ApiStatus;

/**
* Fired to allow mods to register custom {@linkplain BufferBuilder render buffers}.
* This allows to have dedicated {@linkplain BufferBuilder render buffer} for each {@linkplain RenderType render type}
* that can filled and rendered in batch
* This event is fired after the default Minecraft render buffers have been registered.
*
* <p>This event is not {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.</p>
*
* <p>This event is fired on the mod-specific event bus, only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*/
public class RegisterRenderBuffersEvent extends Event implements IModBusEvent {
private final SortedMap<RenderType, BufferBuilder> renderBuffers;

@ApiStatus.Internal
public RegisterRenderBuffersEvent(SortedMap<RenderType, BufferBuilder> renderBuffers) {
this.renderBuffers = renderBuffers;
}

/**
* Registers a default render buffer with buffer size specified in the render type.
*
* @param renderType a render type of the render buffer
*/
public void registerRenderBuffer(RenderType renderType) {
registerRenderBuffer(renderType, new BufferBuilder(renderType.bufferSize()));
}

/**
* Registers a render buffer for specified render type.
*
* @param renderType a render type of the render buffer
* @param renderBuffer a render buffer to register
*/
public void registerRenderBuffer(RenderType renderType, BufferBuilder renderBuffer) {
if (renderBuffers.containsKey(renderType)) {
throw new IllegalStateException("Duplicate attempt to register render buffer: " + renderType);
}

renderBuffers.put(renderType, renderBuffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

package net.neoforged.neoforge.debug.client;

import com.mojang.blaze3d.vertex.BufferBuilder;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.neoforge.client.event.ClientChatEvent;
import net.neoforged.neoforge.client.event.ClientPlayerChangeGameTypeEvent;
import net.neoforged.neoforge.client.event.RegisterRenderBuffersEvent;
import net.neoforged.neoforge.client.event.RenderLevelStageEvent;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
Expand All @@ -31,4 +36,29 @@ static void playerClientChatEvent(final ClientChatEvent event, final DynamicTest
static void clientPlayerChangeGameTypeEvent(final ClientPlayerChangeGameTypeEvent event, final DynamicTest test) {
test.requestConfirmation(Minecraft.getInstance().player, Component.literal("Did you just change your game mode from " + event.getCurrentGameType() + " to " + event.getNewGameType() + "?"));
}

@TestHolder(description = { "Tests if the RegisterRenderBuffersEvent event is fired and whether the registered render buffer is represented within a fixed render buffer map" }, enabledByDefault = true)
static void registerRenderBuffersEvent(final DynamicTest test) {
test.framework().modEventBus().addListener((final RegisterRenderBuffersEvent event) -> {
event.registerRenderBuffer(RenderType.lightning());
});
test.framework().modEventBus().addListener((final RenderLevelStageEvent.RegisterStageEvent event) -> {
try {
var bufferSource = Minecraft.getInstance().renderBuffers().bufferSource();
var field = bufferSource.getClass().getDeclaredField("fixedBuffers");

field.setAccessible(true);

var fixedBuffers = (Map<RenderType, BufferBuilder>) field.get(bufferSource);

if (fixedBuffers != null && fixedBuffers.containsKey(RenderType.lightning())) {
test.pass();
} else {
test.fail("The render buffer for the specified render type was not registered");
}
} catch (Exception e) {
test.fail("Failed to access fixed buffers map");
}
});
}
}

0 comments on commit 5150301

Please sign in to comment.