Skip to content

Commit

Permalink
Merge pull request IrisShaders#1466 from IrisShaders/textSink
Browse files Browse the repository at this point in the history
Text sink API
  • Loading branch information
coderbot16 authored Jun 5, 2022
2 parents 6f9683e + 5f62374 commit d50cfda
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/main/java/net/coderbot/iris/IrisApiV0Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import net.coderbot.iris.pipeline.FixedFunctionWorldRenderingPipeline;
import net.coderbot.iris.pipeline.WorldRenderingPipeline;
import net.coderbot.iris.shadows.ShadowRenderingState;
import net.coderbot.iris.vertices.IrisTextVertexSinkImpl;
import net.irisshaders.iris.api.v0.IrisApi;
import net.irisshaders.iris.api.v0.IrisApiConfig;
import net.irisshaders.iris.api.v0.IrisTextVertexSink;
import net.minecraft.client.gui.screens.Screen;

import java.nio.ByteBuffer;
import java.util.function.IntFunction;

public class IrisApiV0Impl implements IrisApi {
public static final IrisApiV0Impl INSTANCE = new IrisApiV0Impl();
private static final IrisApiV0ConfigImpl CONFIG = new IrisApiV0ConfigImpl();

@Override
public int getMinorApiRevision() {
return 0;
return 1;
}

@Override
Expand Down Expand Up @@ -47,4 +52,9 @@ public String getMainScreenLanguageKey() {
public IrisApiConfig getConfig() {
return CONFIG;
}

@Override
public IrisTextVertexSink createTextVertexSink(int maxQuadCount, IntFunction<ByteBuffer> bufferProvider) {
return new IrisTextVertexSinkImpl(maxQuadCount, bufferProvider);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package net.coderbot.iris.vertices;

import com.mojang.blaze3d.vertex.VertexFormat;
import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.QuadViewEntity;
import net.coderbot.iris.vendored.joml.Vector3f;
import net.irisshaders.iris.api.v0.IrisTextVertexSink;
import org.lwjgl.system.MemoryUtil;

import java.nio.ByteBuffer;
import java.util.function.IntFunction;

public class IrisTextVertexSinkImpl implements IrisTextVertexSink {
static VertexFormat format = IrisVertexFormats.TERRAIN;
private final ByteBuffer buffer;
private final QuadViewEntity.QuadViewEntityUnsafe quad = new QuadViewEntity.QuadViewEntityUnsafe();
private final Vector3f saveNormal = new Vector3f();
private static final int STRIDE = IrisVertexFormats.TERRAIN.getVertexSize();
private int vertexCount;
private long elementOffset;
private float uSum;
private float vSum;

public IrisTextVertexSinkImpl(int maxQuadCount, IntFunction<ByteBuffer> buffer) {
this.buffer = buffer.apply(format.getVertexSize() * 4 * maxQuadCount);
this.elementOffset = MemoryUtil.memAddress(this.buffer);
}

@Override
public VertexFormat getUnderlyingVertexFormat() {
return format;
}

@Override
public ByteBuffer getUnderlyingByteBuffer() {
return buffer;
}

@Override
public void quad(float minX, float minY, float maxX, float maxY, float z, int color, float minU, float minV, float maxU, float maxV, int light) {
vertex(minX, minY, z, color, minU, minV, light);
vertex(minX, maxY, z, color, minU, maxV, light);
vertex(maxX, maxY, z, color, maxU, maxV, light);
vertex(maxX, minY, z, color, maxU, minV, light);
}

private void vertex(float x, float y, float z, int color, float u, float v, int light) {
vertexCount++;
uSum += u;
vSum += v;

long i = elementOffset;

MemoryUtil.memPutFloat(i, x);
MemoryUtil.memPutFloat(i + 4, y);
MemoryUtil.memPutFloat(i + 8, z);
MemoryUtil.memPutInt(i + 12, color);
MemoryUtil.memPutFloat(i + 16, u);
MemoryUtil.memPutFloat(i + 20, v);
MemoryUtil.memPutInt(i + 24, light);

if (vertexCount == 4) {
// TODO: compute this at the head of quad()
vertexCount = 0;
uSum *= 0.25;
vSum *= 0.25;
quad.setup(elementOffset, IrisVertexFormats.TERRAIN.getVertexSize());

NormalHelper.computeFaceNormal(saveNormal, quad);
float normalX = saveNormal.x;
float normalY = saveNormal.y;
float normalZ = saveNormal.z;
int normal = NormalHelper.packNormal(saveNormal, 0.0F);

int tangent = NormalHelper.computeTangent(normalX, normalY, normalZ, quad);

for (long vertex = 0; vertex < 4; vertex++) {
MemoryUtil.memPutFloat(i + 36 - STRIDE * vertex, uSum);
MemoryUtil.memPutFloat(i + 40 - STRIDE * vertex, vSum);
MemoryUtil.memPutInt(i + 28 - STRIDE * vertex, normal);
MemoryUtil.memPutInt(i + 44 - STRIDE * vertex, tangent);
}

uSum = 0;
vSum = 0;
}

buffer.position(buffer.position() + STRIDE);
elementOffset += STRIDE;
}
}
13 changes: 12 additions & 1 deletion src/main/java/net/irisshaders/iris/api/v0/IrisApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.coderbot.iris.IrisApiV0Impl;

import java.nio.ByteBuffer;
import java.util.function.IntFunction;

/**
* The entry point to the Iris API, major version 0. This is currently the latest
* version of the API.
Expand All @@ -22,7 +25,7 @@ static IrisApi getInstance() {
* if they wish to check whether given API calls are available on
* the currently installed Iris version.
*
* @return The current minor revision. Currently, revision 0.
* @return The current minor revision. Currently, revision 1.
*/
int getMinorApiRevision();

Expand Down Expand Up @@ -94,4 +97,12 @@ static IrisApi getInstance() {
* @since API v0.0
*/
IrisApiConfig getConfig();

/**
* Gets a text vertex sink to render into.
* @param maxQuadCount Maximum amount of quads that will be rendered with this sink
* @param bufferProvider An IntFunction that can provide a {@code ByteBuffer} with at minimum the bytes provided by the input parameter
* @since API 0.1
*/
IrisTextVertexSink createTextVertexSink(int maxQuadCount, IntFunction<ByteBuffer> bufferProvider);
}
34 changes: 34 additions & 0 deletions src/main/java/net/irisshaders/iris/api/v0/IrisTextVertexSink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.irisshaders.iris.api.v0;

import com.mojang.blaze3d.vertex.VertexFormat;

import java.nio.ByteBuffer;

public interface IrisTextVertexSink {
/**
* Gets the underlying vertex format used for rendering text.
* @return a valid {@code VertexFormat} instance
*/
VertexFormat getUnderlyingVertexFormat();
/**
* Gets the underlying buffer used for rendering text in the current sink.
* @return a valid {@code ByteBuffer}
*/
ByteBuffer getUnderlyingByteBuffer();

/**
* Writes a singular quad with all vertex attributes needed by the current format into the current {@code ByteBuffer}.
* @param x1 Left-most x coordinate of the quad
* @param y1 Top Y coordinate of the quad
* @param x2 Right-most x coordinate of the quad
* @param y2 Bottom Y coordinate of the quad
* @param z Z coordinate of the quad
* @param color Integer-packed ABGR value, with the equation {@code int color = ((int) (a * 255.0F) & 0xFF) << 24 | ((int) (b * 255.0F) & 0xFF) << 16 | ((int) (g * 255.0F) & 0xFF) << 8 | ((int) (r * 255.0F) & 0xFF)}
* @param u1 Top-left U coordinate of the quad texture
* @param v1 Top-left V coordinate of the quad texture
* @param u2 Bottom-right U coordinate of the quad texture
* @param v2 Bottom right V coordinate of the quad texture
* @param light Integer packed light coordinate
*/
void quad(float x1, float y1, float x2, float y2, float z, int color, float u1, float v1, float u2, float v2, int light);
}

0 comments on commit d50cfda

Please sign in to comment.