forked from maruohon/malilib
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Chicken Variants & introduce
RenderContext
System to replace ol…
…d Tessellator / Buffer Builder workflow
- Loading branch information
1 parent
e7a5c2a
commit 585862a
Showing
11 changed files
with
547 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/fi/dy/masa/malilib/render/RenderContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
src/main/java/fi/dy/masa/malilib/render/RenderPhaseWrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.