Skip to content

Commit

Permalink
Add Chicken Variants & introduce RenderContext System to replace ol…
Browse files Browse the repository at this point in the history
…d Tessellator / Buffer Builder workflow
  • Loading branch information
sakura-ryoko committed Feb 5, 2025
1 parent e7a5c2a commit 585862a
Show file tree
Hide file tree
Showing 11 changed files with 547 additions and 89 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ mod_file_name = malilib-fabric
mod_version = 0.23.999-dev

# Minecraft, Fabric Loader and API and mappings versions
minecraft_version_out = 25w05a
minecraft_version = 25w05a
mappings_version = 25w05a+build.4
minecraft_version_out = 25w06a
minecraft_version = 25w06a
mappings_version = 25w06a+build.1

fabric_loader_version = 0.16.10
mod_menu_version = 13.0.1
fabric_api_version = 0.115.2+1.21.5
fabric_api_version = 0.115.3+1.21.5
17 changes: 16 additions & 1 deletion src/main/java/fi/dy/masa/malilib/gui/GuiColorEditorHSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fi.dy.masa.malilib.config.IConfigInteger;
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler;
import fi.dy.masa.malilib.gui.interfaces.ITextFieldListener;
import fi.dy.masa.malilib.render.RenderContext;
import fi.dy.masa.malilib.render.RenderUtils;
import fi.dy.masa.malilib.util.KeyCodes;
import fi.dy.masa.malilib.util.StringUtils;
Expand Down Expand Up @@ -500,9 +501,13 @@ protected void drawColorSelector()
RenderUtils.drawOutline(cx - 1, cy - 1, cw + 2, ch + 2, 0xC0FFFFFF, z); // current color indicator
RenderUtils.drawOutline(this.xHFullSV, y - 1, this.widthHFullSV, this.sizeHS + 2, 0xC0FFFFFF, z); // Hue vertical/full value

/*
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BuiltBuffer builtBuffer;
*/
RenderContext ctx = new RenderContext(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BufferBuilder buffer = ctx.getBuilder();

RenderUtils.setupBlend();

Expand All @@ -518,15 +523,21 @@ protected void drawColorSelector()

try
{
/*
builtBuffer = buffer.end();
BufferRenderer.drawWithGlobalProgram(builtBuffer);
builtBuffer.close();
*/

ctx.drawWithShaders(buffer.end());
}
catch (Exception ignored) { }

// FIXME
//RenderSystem.setShader(GameRenderer::getPositionColorProgram);
buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
//buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);
ctx.reset();
buffer = ctx.start(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR);

int r = (int) (this.relR * 255f);
int g = (int) (this.relG * 255f);
Expand Down Expand Up @@ -613,9 +624,13 @@ protected void drawColorSelector()

try
{
/*
builtBuffer = buffer.end();
BufferRenderer.drawWithGlobalProgram(builtBuffer);
builtBuffer.close();
*/

ctx.drawWithShaders(buffer.end());
}
catch (Exception ignored) { }

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/fi/dy/masa/malilib/interfaces/IDataSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ default Inventory getEntityInventory(World world, int entityId, boolean useNbt)
}
else if (entity instanceof PlayerEntity player)
{
inv = new SimpleInventory(player.getInventory().main.toArray(new ItemStack[36]));
// TODO get main()
inv = new SimpleInventory(player.getInventory().method_67533().toArray(new ItemStack[36]));
}
else if (entity instanceof VillagerEntity)
{
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/fi/dy/masa/malilib/render/InventoryOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ public class InventoryOverlay
public static void renderInventoryBackground(InventoryRenderType type, int x, int y, int slotsPerRow, int totalSlots, MinecraftClient mc)
{
RenderUtils.setupBlend();
/*
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BuiltBuffer builtBuffer;
*/

RenderContext ctx = new RenderContext(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BufferBuilder buffer = ctx.getBuilder();

RenderSystem.setShader(ShaderProgramKeys.POSITION_TEX);
//RenderSystem.setShader(GameRenderer::getPositionTexProgram);
Expand Down Expand Up @@ -226,9 +231,13 @@ else if (type == InventoryRenderType.FIXED_54)

try
{
/*
builtBuffer = buffer.end();
BufferRenderer.drawWithGlobalProgram(builtBuffer);
builtBuffer.close();
*/

ctx.drawWithShaders(buffer.end());
}
catch (Exception ignored) { }
}
Expand Down Expand Up @@ -346,9 +355,13 @@ public static void renderEquipmentOverlayBackground(int x, int y, LivingEntity e
{
RenderUtils.color(1f, 1f, 1f, 1f);

/*
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BuiltBuffer builtBuffer;
*/
RenderContext ctx = new RenderContext(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
BufferBuilder buffer = ctx.getBuilder();

RenderSystem.setShader(ShaderProgramKeys.POSITION_TEX);
//RenderSystem.setShader(GameRenderer::getPositionTexProgram);
Expand All @@ -372,9 +385,13 @@ public static void renderEquipmentOverlayBackground(int x, int y, LivingEntity e

try
{
/*
builtBuffer = buffer.end();
BufferRenderer.drawWithGlobalProgram(builtBuffer);
builtBuffer.close();
*/

ctx.drawWithShaders(buffer.end());
}
catch (Exception ignored) { }

Expand Down
143 changes: 143 additions & 0 deletions src/main/java/fi/dy/masa/malilib/render/RenderContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package fi.dy.masa.malilib.render;

import javax.annotation.Nullable;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gl.GlUsage;
import net.minecraft.client.gl.VertexBuffer;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BuiltBuffer;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.util.BufferAllocator;

public class RenderContext implements AutoCloseable
{
@Nullable
private VertexBuffer vertex;
private BufferAllocator alloc;
private BufferBuilder builder;

public RenderContext()
{
this.vertex = null;
this.alloc = null;
this.builder = null;
}

public RenderContext(VertexFormat.DrawMode drawMode, VertexFormat format)
{
this.alloc = new BufferAllocator(format.getVertexSizeByte());
this.builder = new BufferBuilder(this.alloc, drawMode, format);
this.vertex = new VertexBuffer(GlUsage.STATIC_WRITE);
}

public RenderContext(VertexFormat.DrawMode drawMode, VertexFormat format, GlUsage usage)
{
this.alloc = new BufferAllocator(format.getVertexSizeByte());
this.builder = new BufferBuilder(this.alloc, drawMode, format);
this.vertex = new VertexBuffer(usage);
}

public BufferBuilder start(VertexFormat.DrawMode drawMode, VertexFormat format)
{
return this.start(drawMode, format, GlUsage.STATIC_WRITE);
}

public BufferBuilder start(VertexFormat.DrawMode drawMode, VertexFormat format, GlUsage usage)
{
this.alloc = new BufferAllocator(format.getVertexSizeByte());
this.builder = new BufferBuilder(this.alloc, drawMode, format);
this.vertex = new VertexBuffer(usage);

return this.builder;
}

public @Nullable BufferBuilder getBuilder()
{
return this.builder;
}

public void draw() throws RuntimeException
{
if (this.vertex == null)
{
throw new RuntimeException("Vertex Buffer is null!");
}

if (RenderSystem.isOnRenderThread())
{
this.ensureSafe();
this.vertex.bind();
this.vertex.upload(this.builder.end());
this.vertex.draw();
VertexBuffer.unbind();
}
}

public void drawWithShaders(BuiltBuffer meshData) throws RuntimeException
{
if (this.vertex == null)
{
throw new RuntimeException("Vertex Buffer is null!");
}

if (RenderSystem.isOnRenderThread())
{
this.ensureSafe();
this.vertex.bind();
this.vertex.upload(meshData);
this.vertex.draw(RenderSystem.getModelViewMatrix(), RenderSystem.getProjectionMatrix(), RenderSystem.getShader());
VertexBuffer.unbind();
meshData.close();
}
}

private void ensureSafe() throws RuntimeException
{
if (this.vertex == null)
{
throw new RuntimeException("Vertex Buffer is null!");
}

if (this.vertex.isClosed())
{
throw new RuntimeException("Vertex Buffer is closed!");
}

if (this.alloc == null)
{
throw new RuntimeException("Allocator not valid!");
}

if (this.builder == null)
{
throw new RuntimeException("Buffer Builder not valid!");
}
}

public void reset()
{
if (this.vertex != null && !this.vertex.isClosed())
{
this.vertex.close();
this.vertex = null;
}

if (this.alloc != null)
{
this.alloc.close();
this.builder = null;
}

if (this.builder != null)
{
this.builder = null;
}
}

@Override
public void close() throws Exception
{
this.reset();
}
}
67 changes: 67 additions & 0 deletions src/main/java/fi/dy/masa/malilib/render/RenderPhaseWrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fi.dy.masa.malilib.render;

import javax.annotation.Nullable;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gl.VertexBuffer;
import net.minecraft.client.render.BuiltBuffer;
import net.minecraft.client.render.RenderPhase;
import net.minecraft.client.render.VertexFormat;

public class RenderPhaseWrap extends RenderPhase
{
@Nullable
private static VertexBuffer buffer;

public RenderPhaseWrap(String name, Runnable start, Runnable stop)
{
super(name, start, stop);
}

public static void drawWithShaders(BuiltBuffer buffer)
{
RenderSystem.assertOnRenderThreadOrInit();
VertexBuffer vertex = upload(buffer);
vertex.draw(RenderSystem.getModelViewMatrix(), RenderSystem.getProjectionMatrix(), RenderSystem.getShader());
}

public static void reset()
{
if (buffer != null)
{
buffer.close();
buffer = null;
VertexBuffer.unbind();
}
}

public static void draw(BuiltBuffer buffer)
{
RenderSystem.assertOnRenderThreadOrInit();
VertexBuffer vertex = upload(buffer);
vertex.draw();
}

private static VertexBuffer upload(BuiltBuffer buffer)
{
VertexBuffer vertex = bind(buffer.getDrawParameters().format());
vertex.upload(buffer);
return vertex;
}

private static VertexBuffer bind(VertexFormat fmt)
{
VertexBuffer vertex = fmt.getBuffer();
bind(vertex);
return vertex;
}

private static void bind(VertexBuffer vertex)
{
if (vertex != buffer)
{
vertex.bind();
buffer = vertex;
}
}
}
Loading

0 comments on commit 585862a

Please sign in to comment.