forked from IrisShaders/Iris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request IrisShaders#1466 from IrisShaders/textSink
Text sink API
- Loading branch information
Showing
4 changed files
with
147 additions
and
2 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
90 changes: 90 additions & 0 deletions
90
src/main/java/net/coderbot/iris/vertices/IrisTextVertexSinkImpl.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,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; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/net/irisshaders/iris/api/v0/IrisTextVertexSink.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,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); | ||
} |