Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new render state middleware #1574

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a780c8
Add render state middleware
Argon4W Oct 1, 2024
3a5138c
Add render state middleware
Argon4W Oct 1, 2024
85c1e62
Add javadoc for AddSectionGeometryEvent*SectionRenderingContext#getOr…
Argon4W Oct 1, 2024
ff34b2e
Add a separate function for get or create chunk buffer from bufer def…
Argon4W Oct 1, 2024
86c2a2c
Rename and put chunk buffer definition registeration logic into Clien…
Argon4W Oct 2, 2024
4aabbc2
Add more general params, remove LegacyRenderTypeBufferDefinition
Argon4W Oct 2, 2024
091e4e6
Rename
Argon4W Oct 3, 2024
4d7b36f
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 9, 2024
3d524c4
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 13, 2024
389fb9f
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 13, 2024
36dead9
Fix chunk buffer definition logic
Argon4W Oct 13, 2024
bdc9229
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 15, 2024
f986a46
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 17, 2024
b0d972c
Merge branch '1.21.x' into 1.21.x-render-middleware
Argon4W Oct 22, 2024
9b2ae91
Fix patch and port to 1.21.2
Argon4W Oct 23, 2024
fb72013
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 24, 2024
48e6517
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 25, 2024
105daeb
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 26, 2024
8b99c43
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 29, 2024
07cba15
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Oct 31, 2024
d5b266b
Merge branch 'neoforged:1.21.x' into 1.21.x-render-middleware
Argon4W Nov 9, 2024
986e363
Merge branch '1.21.x' into 1.21.x-render-middleware
Argon4W Dec 4, 2024
db99e11
Fix RenderType and MultiBufferSource patches
Argon4W Dec 4, 2024
d6e8206
Merge branch '1.21.x' into 1.21.x-render-middleware
Argon4W Dec 4, 2024
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
11 changes: 11 additions & 0 deletions patches/net/minecraft/client/renderer/MultiBufferSource.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- a/net/minecraft/client/renderer/MultiBufferSource.java
+++ b/net/minecraft/client/renderer/MultiBufferSource.java
@@ -14,7 +_,7 @@
import net.neoforged.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
-public interface MultiBufferSource {
+public interface MultiBufferSource extends net.neoforged.neoforge.client.extensions.IMultiBufferSourceExtension {
static MultiBufferSource.BufferSource immediate(ByteBufferBuilder p_350664_) {
return immediateWithBuffers(Object2ObjectSortedMaps.emptyMap(), p_350664_);
}
32 changes: 29 additions & 3 deletions patches/net/minecraft/client/renderer/RenderType.java.patch
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
--- a/net/minecraft/client/renderer/RenderType.java
+++ b/net/minecraft/client/renderer/RenderType.java
@@ -1847,4 +_,16 @@
@@ -1010,6 +_,15 @@
.createCompositeState(false)
);
public static final ImmutableList<RenderType> CHUNK_BUFFER_LAYERS = ImmutableList.of(solid(), cutoutMipped(), cutout(), translucent(), tripwire());
+ public static final ImmutableList<RenderType> NEOFORGE_CHUNK_BUFFER_LAYERS;
+ static {
+ net.neoforged.neoforge.client.buffer.chunk.ChunkBufferDefinitionManager.init();
+ ImmutableList.Builder<RenderType> builder = ImmutableList.builder();
+ builder.addAll(CHUNK_BUFFER_LAYERS);
+ builder.addAll(net.neoforged.neoforge.client.buffer.chunk.ChunkBufferDefinitionManager.getChunkBufferDefinitions().stream().map(net.neoforged.neoforge.client.buffer.VanillaBufferDefinitions::bakeVanillaRenderType).toList());
+ NEOFORGE_CHUNK_BUFFER_LAYERS = builder.build();
+ }
+
public final VertexFormat format;
public final VertexFormat.Mode mode;
public final int bufferSize;
@@ -1545,7 +_,7 @@
}

public static List<RenderType> chunkBufferLayers() {
- return CHUNK_BUFFER_LAYERS;
+ return NEOFORGE_CHUNK_BUFFER_LAYERS;
}

public int bufferSize() {
@@ -1846,5 +_,17 @@
public String toString() {
return this.name;
}
}
+ }
+
+ // Neo: Assign internal IDs for RenderType to be used in rendering
+ private int chunkLayerId = -1;
Expand All @@ -15,5 +41,5 @@
+ int i = 0;
+ for (var layer : chunkBufferLayers())
+ layer.chunkLayerId = i++;
+ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
for (Entry<RenderType, BufferBuilder> entry : map.entrySet()) {
RenderType rendertype1 = entry.getKey();
MeshData meshdata = entry.getValue().build();
@@ -105,7 +_,8 @@
BufferBuilder bufferbuilder = p_350651_.get(p_350494_);
if (bufferbuilder == null) {
ByteBufferBuilder bytebufferbuilder = p_350965_.buffer(p_350494_);
- bufferbuilder = new BufferBuilder(bytebufferbuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK);
+ //Neo: build BufferBuilder based on RenderType's format and mode
+ bufferbuilder = new BufferBuilder(bytebufferbuilder, p_350494_.mode, p_350494_.format);
p_350651_.put(p_350494_, bufferbuilder);
}

@@ -115,9 +_,10 @@
private <E extends BlockEntity> void handleBlockEntity(SectionCompiler.Results p_350754_, E p_350386_) {
BlockEntityRenderer<E> blockentityrenderer = this.blockEntityRenderer.getRenderer(p_350386_);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
import net.neoforged.fml.ModLoader;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.asm.enumextension.ExtensionInfo;
import net.neoforged.neoforge.client.buffer.VanillaBufferDefinitions;
import net.neoforged.neoforge.client.buffer.chunk.ChunkBufferDefinitionManager;
import net.neoforged.neoforge.client.buffer.chunk.IChunkBufferCallback;
import net.neoforged.neoforge.client.buffer.chunk.ISectionLayerRenderer;
import net.neoforged.neoforge.client.entity.animation.json.AnimationTypeManager;
import net.neoforged.neoforge.client.event.AddSectionGeometryEvent;
import net.neoforged.neoforge.client.event.CalculateDetachedCameraDistanceEvent;
Expand Down Expand Up @@ -273,11 +277,32 @@ public static boolean onDrawHighlight(LevelRenderer context, Camera camera, Bloc
public static void dispatchRenderStage(RenderLevelStageEvent.Stage stage, LevelRenderer levelRenderer, @Nullable PoseStack poseStack, Matrix4f modelViewMatrix, Matrix4f projectionMatrix, int renderTick, Camera camera, Frustum frustum) {
var mc = Minecraft.getInstance();
var profiler = Profiler.get();

profiler.push(stage.toString());
NeoForge.EVENT_BUS.post(new RenderLevelStageEvent(stage, levelRenderer, poseStack, modelViewMatrix, projectionMatrix, renderTick, mc.getDeltaTracker(), camera, frustum));
dispatchCustomChunkBuffers(stage, levelRenderer, modelViewMatrix, projectionMatrix, camera.getPosition().toVector3f());
profiler.pop();
}

public static void dispatchCustomChunkBuffers(RenderLevelStageEvent.Stage stage, LevelRenderer levelRenderer, Matrix4f modelViewMatrix, Matrix4f projectionMatrix, Vector3f position) {
ChunkBufferDefinitionManager.getChunkBufferDefinitions(stage).forEach(entry -> {
RenderType renderType = VanillaBufferDefinitions.bakeVanillaRenderType(entry.bufferDefinition());
IChunkBufferCallback callback = entry.callback();
ISectionLayerRenderer sectionLayerRenderer = ISectionLayerRenderer.vanilla(renderType, levelRenderer);

callback.onRenderChunkBuffer(
sectionLayerRenderer,
levelRenderer,
entry.bufferDefinition(),
position.x,
position.y,
position.z,
modelViewMatrix,
projectionMatrix);
levelRenderer.renderBuffers.bufferSource().endBatch(renderType);
});
}

public static void dispatchRenderStage(RenderType renderType, LevelRenderer levelRenderer, Matrix4f modelViewMatrix, Matrix4f projectionMatrix, int renderTick, Camera camera, Frustum frustum) {
RenderLevelStageEvent.Stage stage = RenderLevelStageEvent.Stage.fromRenderType(renderType);
if (stage != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.buffer;

import com.google.common.collect.ImmutableMap;
import java.util.Optional;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.client.buffer.param.BufferDefinitionParamTypeManager;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParam;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParamType;

/**
* The default implementation of {@link IBufferDefinition}. Stores params in an {@link ImmutableMap}
*/
public class DefaultBufferDefinition implements IBufferDefinition {
private final ImmutableMap<IBufferDefinitionParamType<?, ?>, IBufferDefinitionParam<?>> params;

public DefaultBufferDefinition(ImmutableMap<IBufferDefinitionParamType<?, ?>, IBufferDefinitionParam<?>> params) {
this.params = params;
}

public DefaultBufferDefinition() {
this.params = ImmutableMap.of();
}

@Override
public boolean hasParam(IBufferDefinitionParamType<?, ?> param) {
return params.containsKey(param);
}

@SuppressWarnings("unchecked")
@Override
public <T, P extends IBufferDefinitionParam<T>> Optional<T> getParam(IBufferDefinitionParamType<T, P> paramType) {
return Optional.ofNullable(params.get(paramType)).map(param -> (T) param.getValue());
}

@SuppressWarnings("unchecked")
@Override
public <T, P extends IBufferDefinitionParam<T>> T getParamOrDefault(IBufferDefinitionParamType<T, P> type) {
return (T) params.getOrDefault(type, type.getDefaultValue()).getValue();
}

@Override
public ImmutableMap<IBufferDefinitionParamType<?, ?>, IBufferDefinitionParam<?>> getParams() {
return params;
}

/**
* Create and returns the {@link IBufferDefinitionBuilder} implementation for {@link IBufferDefinition}
*
* @return the builder created
*/
public static IBufferDefinitionBuilder builder() {
return new Builder();
}

public static class Builder implements IBufferDefinitionBuilder {
private final ImmutableMap.Builder<IBufferDefinitionParamType<?, ?>, IBufferDefinitionParam<?>> params;

private Builder() {
this.params = ImmutableMap.builder();
}

@Override
public IBufferDefinitionBuilder withParam(IBufferDefinitionParamType<?, ?> type, IBufferDefinitionParam<?> param) {
params.put(type, param);
return this;
}

@Override
public IBufferDefinitionBuilder withParam(ResourceLocation resourceLocation, IBufferDefinitionParam<?> param) {
getParamType(resourceLocation).ifPresent(type -> params.put(type, param));
return this;
}

@Override
public IBufferDefinitionBuilder withParam(IBufferDefinitionParam<?> param) {
params.put(param.getType(), param);
return this;
}

@Override
public IBufferDefinition build() {
return new DefaultBufferDefinition(params.build());
}

private Optional<IBufferDefinitionParamType<?, ?>> getParamType(ResourceLocation resourceLocation) {
return Optional.ofNullable(BufferDefinitionParamTypeManager.getParamType(resourceLocation));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.buffer;

import com.google.common.collect.ImmutableMap;
import java.util.Optional;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParam;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParamType;

public interface IBufferDefinition {
boolean hasParam(IBufferDefinitionParamType<?, ?> param);

<T, P extends IBufferDefinitionParam<T>> Optional<T> getParam(IBufferDefinitionParamType<T, P> paramType);

<T, P extends IBufferDefinitionParam<T>> T getParamOrDefault(IBufferDefinitionParamType<T, P> type);

ImmutableMap<IBufferDefinitionParamType<?, ?>, IBufferDefinitionParam<?>> getParams();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.buffer;

import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParam;
import net.neoforged.neoforge.client.buffer.param.IBufferDefinitionParamType;
import net.neoforged.neoforge.client.buffer.param.general.IGeneralUsageParam;
import net.neoforged.neoforge.client.event.RegisterBufferDefinitionParamTypeAliasesEvent;

public interface IBufferDefinitionBuilder {
/**
* Add a {@link IBufferDefinitionParam} to the {@link IBufferDefinition} with a specified {@link IBufferDefinitionParamType}.
* Can be used for {@link IGeneralUsageParam}.
*
* @param type the param type
* @param param the param value
* @return the builder itself
*/
IBufferDefinitionBuilder withParam(IBufferDefinitionParamType<?, ?> type, IBufferDefinitionParam<?> param);

/**
* Add a {@link IBufferDefinitionParam} to the {@link IBufferDefinition} with a param type described in a {@link ResourceLocation}.
* It can specify param type without accessing the {@link IBufferDefinitionParamType} instance. (e.g. provided by other mods)
* Generally used for {@link IGeneralUsageParam}.
*
* @param resourceLocation the registered location of the param type
* @param param the param value
* @return the builder itself
* @see RegisterBufferDefinitionParamTypeAliasesEvent
*/
IBufferDefinitionBuilder withParam(ResourceLocation resourceLocation, IBufferDefinitionParam<?> param);

/**
* Add a {@link IBufferDefinitionParam} to the {@link IBufferDefinition} with {@link IBufferDefinitionParamType} automatically determined by {@link IBufferDefinitionParam#getType()}.
* <STRONG>CANNOT</STRONG> be used for {@link IGeneralUsageParam}.
*
* @param param the param value
* @return the builder itself
* @see IGeneralUsageParam#getType()
*/
IBufferDefinitionBuilder withParam(IBufferDefinitionParam<?> param);

/**
* Build the {@link IBufferDefinition} with params declared.
*
* @return the built buffer definition
*/
IBufferDefinition build();
}
Loading
Loading