MOre work on the ui renderer

This commit is contained in:
Speiger 2025-04-21 22:31:59 +02:00
parent 8eb4314952
commit 814e35a663
11 changed files with 119 additions and 39 deletions

View File

@ -16,6 +16,11 @@ public class Matrix4fStack extends Matrix4f {
load(stack.pop()); load(stack.pop());
} }
public void popRoot() {
load(stack.peek(stack.size()-1));
stack.clear();
}
public int size() { public int size() {
return stack.size(); return stack.size();
} }

View File

@ -7,6 +7,7 @@ import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.math.vector.quaternion.Quaternion; import speiger.src.coreengine.math.vector.quaternion.Quaternion;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox; import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.models.DrawCall; import speiger.src.coreengine.rendering.models.DrawCall;
import speiger.src.coreengine.rendering.tesselation.format.VertexFormat;
import speiger.src.coreengine.rendering.textures.base.ITexture; import speiger.src.coreengine.rendering.textures.base.ITexture;
public interface IUIRenderer { public interface IUIRenderer {
@ -31,7 +32,7 @@ public interface IUIRenderer {
public void rotate(Quaternion rotation); public void rotate(Quaternion rotation);
public void drawCustom(Consumer<Matrix4f> matrix); public void drawCustom(Consumer<Matrix4f> matrix);
public void drawCustom(DrawCall drawcall); public void drawCustom(VertexFormat format, DrawCall drawcall);
public void drawLine(float minX, float minY, float maxX, float maxY, float zLevel, int color); public void drawLine(float minX, float minY, float maxX, float maxY, float zLevel, int color);

View File

@ -3,31 +3,30 @@ package speiger.src.coreengine.rendering.gui.renderer;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import speiger.src.collections.ints.collections.IntStack;
import speiger.src.collections.ints.lists.IntArrayList;
import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.utils.Stack;
import speiger.src.coreengine.math.misc.Facing; import speiger.src.coreengine.math.misc.Facing;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.math.vector.matrix.Matrix4f; import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.math.vector.matrix.Matrix4fStack; import speiger.src.coreengine.math.vector.matrix.Matrix4fStack;
import speiger.src.coreengine.math.vector.quaternion.Quaternion; import speiger.src.coreengine.math.vector.quaternion.Quaternion;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox; import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.models.DrawCall; import speiger.src.coreengine.rendering.models.DrawCall;
import speiger.src.coreengine.rendering.tesselation.GLCall;
import speiger.src.coreengine.rendering.tesselation.buffer.VertexBuilder; import speiger.src.coreengine.rendering.tesselation.buffer.VertexBuilder;
import speiger.src.coreengine.rendering.tesselation.format.VertexFormat;
import speiger.src.coreengine.rendering.tesselation.format.VertexTypes;
import speiger.src.coreengine.rendering.utils.values.GLMode;
public class SimpleUIRenderer implements IUIRenderer, AutoCloseable { public class SimpleUIRenderer implements IUIRenderer, AutoCloseable {
TexturedRect cachedRect = new TexturedRect(); TexturedRect cachedRect = new TexturedRect();
Matrix4fStack transform = new Matrix4fStack(); Matrix4fStack transform = new Matrix4fStack();
List<Matrix4f> transformCache = new ObjectArrayList<>(); List<Matrix4f> transformCache = new ObjectArrayList<>(new Matrix4f());
IntStack indexCache = new IntArrayList(); int currentMatrix = 0;
boolean matrixChanged = false; int lastTexture = 0;
List<UIDrawCall> drawCalls = new ObjectArrayList<>();
List<GLCall> drawCalls = new ObjectArrayList<>();
Stack<GLCall> cache = new ObjectArrayList<>();
VertexBuilder builder = new VertexBuilder(Short.MAX_VALUE); VertexBuilder builder = new VertexBuilder(Short.MAX_VALUE);
@Override @Override
public void close() throws Exception { public void close() throws Exception {
builder.close(); builder.close();
@ -59,86 +58,126 @@ public class SimpleUIRenderer implements IUIRenderer, AutoCloseable {
@Override @Override
public void pushTransform() { public void pushTransform() {
transform.push(); transform.push();
invalidateMatrix();
} }
@Override @Override
public void popTransform() { public void popTransform() {
transform.pop(); transform.pop();
if(transform.size() > indexCache.size()) { invalidateMatrix();
indexCache.pop();
}
} }
@Override @Override
public void translate(float z) { public void translate(float z) {
transform.translate(0, 0, z); transform.translate(0, 0, z);
matrixChanged = true; invalidateMatrix();
} }
@Override @Override
public void translate(float x, float y) { public void translate(float x, float y) {
transform.translate(x, y); transform.translate(x, y);
matrixChanged = true; invalidateMatrix();
} }
@Override @Override
public void translate(float x, float y, float z) { public void translate(float x, float y, float z) {
transform.translate(x, y, z); transform.translate(x, y, z);
matrixChanged = true; invalidateMatrix();
} }
@Override @Override
public void scale(float scale) { public void scale(float scale) {
transform.scale(scale); transform.scale(scale);
matrixChanged = true; invalidateMatrix();
} }
@Override @Override
public void scale(float x, float y) { public void scale(float x, float y) {
transform.scale(x, y, 1F); transform.scale(x, y, 1F);
matrixChanged = true; invalidateMatrix();
} }
@Override @Override
public void rotate(Quaternion rotation) { public void rotate(Quaternion rotation) {
transform.rotate(rotation); transform.rotate(rotation);
matrixChanged = true; invalidateMatrix();
}
protected void invalidateMatrix() {
pushDrawcall();
currentMatrix = -1;
}
protected void validateMatrix() {
if(currentMatrix != -1) return;
for(int i = transformCache.size();i>=0;i--) {
if(transformCache.get(i).equals(transform)) {
currentMatrix = i;
return;
}
}
currentMatrix = transformCache.size();
transformCache.add(transform.copyCurrent());
}
protected void ensureDrawCall(GLMode mode, VertexFormat format, int texture) {
if(!builder.is(mode, format) || lastTexture != texture) {
pushDrawcall();
lastTexture = texture;
builder.start(mode, format);
}
}
private void pushDrawcall() {
if(builder.hasData()) {
int start = builder.size();
drawCalls.add(new UIDrawCall(builder.getMode(), start, builder.last()-start, lastTexture, currentMatrix, Vec4f.ZERO, 0F));
builder.endDrawCall(false);
}
} }
@Override @Override
public void drawCustom(Consumer<Matrix4f> matrix) { public void drawCustom(Consumer<Matrix4f> matrix) {
validateMatrix();
} }
@Override @Override
public void drawCustom(DrawCall drawcall) { public void drawCustom(VertexFormat format, DrawCall drawcall) {
validateMatrix();
ensureDrawCall(drawcall.glType(), format, drawcall.textureId());
builder.putBulk(drawcall.data());
} }
@Override @Override
public void drawLine(float minX, float minY, float maxX, float maxY, float zLevel, int color) { public void drawLine(float minX, float minY, float maxX, float maxY, float zLevel, int color) {
validateMatrix();
ensureDrawCall(GLMode.TRIANGLES, VertexTypes.POS_RGBA, 0);
} }
@Override @Override
public void drawFrame(float minX, float minY, float maxX, float maxY, float zLevel, int color) { public void drawFrame(float minX, float minY, float maxX, float maxY, float zLevel, int color) {
validateMatrix();
ensureDrawCall(GLMode.QUADS, VertexTypes.POS_RGBA, 0);
} }
@Override @Override
public void drawRect(float minX, float minY, float maxX, float maxY, float zLevel, int color) { public void drawRect(float minX, float minY, float maxX, float maxY, float zLevel, int color) {
validateMatrix();
ensureDrawCall(GLMode.QUADS, VertexTypes.POS_RGBA, 0);
} }
@Override @Override
public void drawGradientRect(float minX, float minY, float maxX, float maxY, float zLevel, int startColor, int endColor, Facing facing) { public void drawGradientRect(float minX, float minY, float maxX, float maxY, float zLevel, int startColor, int endColor, Facing facing) {
validateMatrix();
ensureDrawCall(GLMode.QUADS, VertexTypes.POS_RGBA, 0);
} }
@Override @Override
public void drawTexturedRect(float minX, float minY, float maxX, float maxY, float zLevel, int texture, float minU, float minV, float maxU, float maxV, int color) { public void drawTexturedRect(float minX, float minY, float maxX, float maxY, float zLevel, int texture, float minU, float minV, float maxU, float maxV, int color) {
validateMatrix();
ensureDrawCall(GLMode.QUADS, VertexTypes.POS_TEX_RGBA, texture);
} }
} }

View File

@ -0,0 +1,8 @@
package speiger.src.coreengine.rendering.gui.renderer;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.rendering.utils.values.GLMode;
public record UIDrawCall(GLMode mode, int start, int length, int texture, int matrix, Vec4f frame, float roundness) {
}

View File

@ -396,7 +396,7 @@ public class UIRenderer implements IReloadableResource
applyAlignment(0, 0, width, height, 0, alignHelper); applyAlignment(0, 0, width, height, 0, alignHelper);
for(DrawCall call : drawCalls) for(DrawCall call : drawCalls)
{ {
ensureDrawing(call.glType(), call.textureId() > 0); ensureDrawing(call.glType().glValue(), call.textureId() > 0);
FloatBuffer buffer = ByteBuffer.wrap(call.data()).order(ByteOrder.nativeOrder()).asFloatBuffer(); FloatBuffer buffer = ByteBuffer.wrap(call.data()).order(ByteOrder.nativeOrder()).asFloatBuffer();
for(int i = 0,m=buffer.remaining();i+9<=m;i+=9) for(int i = 0,m=buffer.remaining();i+9<=m;i+=9)
{ {

View File

@ -4,10 +4,12 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.List; import java.util.List;
public record DrawCall(int glType, int textureId, byte[] data, int size) { import speiger.src.coreengine.rendering.utils.values.GLMode;
public record DrawCall(GLMode glType, int textureId, byte[] data, int size) {
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
public DrawCall(int glType, int textureId, float[] data, int size) { public DrawCall(GLMode glType, int textureId, float[] data, int size) {
this(glType, textureId, convert(data), size); this(glType, textureId, convert(data), size);
} }

View File

@ -9,7 +9,7 @@ import speiger.src.coreengine.rendering.utils.values.GLMode;
public interface IVertexBuilder extends IVertexBuffer { public interface IVertexBuilder extends IVertexBuffer {
public VertexElement current(); public VertexElement current();
public IVertexBuilder nextElement(); public IVertexBuilder nextElement();
public IVertexBuilder endDrawCall(); public IVertexBuilder endDrawCall(boolean store);
public List<BufferDrawCall> getDrawCalls(); public List<BufferDrawCall> getDrawCalls();
public IVertexBuilder put(int offset, byte value); public IVertexBuilder put(int offset, byte value);

View File

@ -42,6 +42,10 @@ public class VertexBuilder implements IVertexBuilder, AutoCloseable {
return this; return this;
} }
public boolean is(GLMode mode, VertexFormat format) { return this.mode == mode && this.format == format; }
public GLMode getMode() { return mode; }
public VertexFormat getFormat() { return format; }
private void ensureCapacity(int newValue) { private void ensureCapacity(int newValue) {
if(totalStoredBytes + newValue >= buffer.capacity()) { if(totalStoredBytes + newValue >= buffer.capacity()) {
int oldSize = buffer.capacity(); int oldSize = buffer.capacity();
@ -82,9 +86,11 @@ public class VertexBuilder implements IVertexBuilder, AutoCloseable {
return this; return this;
} }
public VertexBuilder endDrawCall() { public VertexBuilder endDrawCall(boolean store) {
if(store) {
if(drawCalls == null) drawCalls = new ObjectArrayList<>(); if(drawCalls == null) drawCalls = new ObjectArrayList<>();
drawCalls.add(new BufferDrawCall(mode, lastVertecies, vertecies)); drawCalls.add(new BufferDrawCall(mode, lastVertecies, vertecies));
}
lastVertecies = vertecies; lastVertecies = vertecies;
return this; return this;
} }
@ -110,6 +116,8 @@ public class VertexBuilder implements IVertexBuilder, AutoCloseable {
@Override @Override
public int size() { return vertecies; } public int size() { return vertecies; }
public int last() { return lastVertecies; }
public boolean hasData() { return vertecies > lastVertecies; }
@Override @Override
public IVertexBuilder put(int offset, byte value) { public IVertexBuilder put(int offset, byte value) {
@ -139,6 +147,14 @@ public class VertexBuilder implements IVertexBuilder, AutoCloseable {
return this; return this;
} }
public VertexBuilder putBulk(byte[] source) {
ensureCapacity(source.length + format.totalOffset());
buffer.position(totalStoredBytes).put(source).position(0);
vertecies += source.length / format.totalOffset();
totalStoredBytes += source.length;
return this;
}
public VertexBuilder putBulk(ByteBuffer source) { public VertexBuilder putBulk(ByteBuffer source) {
ensureCapacity(source.limit() + format.totalOffset()); ensureCapacity(source.limit() + format.totalOffset());
buffer.position(totalStoredBytes).put(source).position(0); buffer.position(totalStoredBytes).put(source).position(0);

View File

@ -10,7 +10,8 @@ public class VertexTypes {
public static final VertexElement RGBA = new VertexElement(4, Usage.COLOR); public static final VertexElement RGBA = new VertexElement(4, Usage.COLOR);
public static final VertexElement NORMAL = new VertexElement(3, Usage.NORMAL); public static final VertexElement NORMAL = new VertexElement(3, Usage.NORMAL);
public static final VertexFormat POS_RGB_3D = VertexFormat.builder().add(POSITION_3D).add(RGB).build(); public static final VertexFormat POS_RGB = VertexFormat.builder().add(POSITION_3D).add(RGB).build();
public static final VertexFormat POS_RGBA_3D = VertexFormat.builder().add(POSITION_3D).add(RGBA).build(); public static final VertexFormat POS_RGBA = VertexFormat.builder().add(POSITION_3D).add(RGBA).build();
public static final VertexFormat POS_TEX_RGBA = VertexFormat.builder().add(POSITION_3D).add(TEXTURE).add(RGBA).build();
public static final VertexFormat TESTING = VertexFormat.builder().add(POSITION_3D).add(TEXTURE).add(RGBA).build(); public static final VertexFormat TESTING = VertexFormat.builder().add(POSITION_3D).add(TEXTURE).add(RGBA).build();
} }

View File

@ -8,6 +8,7 @@ import speiger.src.coreengine.rendering.models.DrawCall;
import speiger.src.coreengine.rendering.tesselation.format.VertexElement; import speiger.src.coreengine.rendering.tesselation.format.VertexElement;
import speiger.src.coreengine.rendering.tesselation.format.VertexFormat; import speiger.src.coreengine.rendering.tesselation.format.VertexFormat;
import speiger.src.coreengine.rendering.tesselation.format.VertexElement.Usage; import speiger.src.coreengine.rendering.tesselation.format.VertexElement.Usage;
import speiger.src.coreengine.rendering.utils.values.GLMode;
public class Tesselator implements IVertexBuilder public class Tesselator implements IVertexBuilder
{ {
@ -202,7 +203,7 @@ public class Tesselator implements IVertexBuilder
public DrawCall getDrawCall(int texture) { public DrawCall getDrawCall(int texture) {
float[] data = new float[currentList.totalOffset() * vertexes]; float[] data = new float[currentList.totalOffset() * vertexes];
buffer.get(data); buffer.get(data);
return new DrawCall(drawType, texture, data, vertexes); return new DrawCall(GLMode.byId(drawType), texture, data, vertexes);
} }
public boolean isDrawing() { return drawing; } public boolean isDrawing() { return drawing; }

View File

@ -29,6 +29,13 @@ public enum GLMode implements IGLMode {
GL11.glDrawElements(glMode, length, glMode, length); GL11.glDrawElements(glMode, length, glMode, length);
} }
public static GLMode byId(int id) {
for(GLMode value : GLMode.values()) {
if(value.glValue() == id) return value;
}
throw new ArrayIndexOutOfBoundsException("GL["+id+"] isn't a valid GL Draw Mode");
}
@Override @Override
public int glValue() { return glMode; } public int glValue() { return glMode; }
@Override @Override