Uploading latest changes

This commit is contained in:
Speiger 2024-08-07 20:23:26 +02:00
parent 7a7d9c1fd0
commit 5fd1ff4904
27 changed files with 171 additions and 147 deletions

View File

@ -1,3 +1,4 @@
# Tests that should be done. # Tests that should be done.
- Test starting animations with delays of each other to see if they act properly. - Test starting animations with delays of each other to see if they act properly.
- Reafactor the GL_TEXTURE_FORMAT because it's in desperate need of an refactor.

View File

@ -62,6 +62,7 @@ dependencies {
implementation "org.lwjgl:lwjgl-opengl" implementation "org.lwjgl:lwjgl-opengl"
implementation "org.lwjgl:lwjgl-stb" implementation "org.lwjgl:lwjgl-stb"
implementation "org.lwjgl:lwjgl-nfd" implementation "org.lwjgl:lwjgl-nfd"
implementation "org.lwjgl:lwjgl-freetype"
implementation "org.lwjgl:lwjgl-nanovg" implementation "org.lwjgl:lwjgl-nanovg"
implementation "org.lwjgl:lwjgl::$lwjglNatives" implementation "org.lwjgl:lwjgl::$lwjglNatives"
implementation "org.lwjgl:lwjgl-glfw::$lwjglNatives" implementation "org.lwjgl:lwjgl-glfw::$lwjglNatives"
@ -70,8 +71,11 @@ dependencies {
implementation "org.lwjgl:lwjgl-opengl::$lwjglNatives" implementation "org.lwjgl:lwjgl-opengl::$lwjglNatives"
implementation "org.lwjgl:lwjgl-stb::$lwjglNatives" implementation "org.lwjgl:lwjgl-stb::$lwjglNatives"
implementation "org.lwjgl:lwjgl-nfd::$lwjglNatives" implementation "org.lwjgl:lwjgl-nfd::$lwjglNatives"
implementation "org.lwjgl:lwjgl-freetype::$lwjglNatives"
implementation "org.lwjgl:lwjgl-nanovg::$lwjglNatives" implementation "org.lwjgl:lwjgl-nanovg::$lwjglNatives"
//Gson //Gson
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.google.code.gson:gson:2.8.6'

View File

@ -1,4 +1,4 @@
org.gradle.jvmargs=-Xmx2G org.gradle.jvmargs=-Xmx2G
lwjglVersion = 3.3.3 lwjglVersion = 3.3.4
lwjglNatives = natives-windows lwjglNatives = natives-windows

View File

@ -69,7 +69,7 @@ public class NewInputTest {
Joystick.INSTANCE.init(manager, bus); Joystick.INSTANCE.init(manager, bus);
FileDrop.INSTANCE.init(bus); FileDrop.INSTANCE.init(bus);
manager.addDevices(Mouse.INSTANCE, Keyboard.INSTANCE, Joystick.INSTANCE, FileDrop.INSTANCE); manager.addDevices(Mouse.INSTANCE, Keyboard.INSTANCE, Joystick.INSTANCE, FileDrop.INSTANCE);
Window window = manager.builder().title("Testing Engine").build(); Window window = manager.builder().title("Testing Engine").antialis(16).build();
shaderTest.register(); shaderTest.register();
guiShader.register(); guiShader.register();
assets.addListener(GLStateTracker.instance().shaders); assets.addListener(GLStateTracker.instance().shaders);
@ -82,6 +82,7 @@ public class NewInputTest {
IFontProvider provider = STBTrueTypeProvider.create(AssetLocation.of("font/roboto/font.json"), assets); IFontProvider provider = STBTrueTypeProvider.create(AssetLocation.of("font/roboto/font.json"), assets);
guiShader.get().proView.set(new Matrix4f().ortho(0, 0, window.width(), window.height(), 1000, -1000)); guiShader.get().proView.set(new Matrix4f().ortho(0, 0, window.width(), window.height(), 1000, -1000));
guiShader.get().model.set(new Matrix4f().scale(1.1F));
int size = 512; int size = 512;
int half = size >> 1; int half = size >> 1;
@ -101,7 +102,7 @@ public class NewInputTest {
// texture.bind(); // texture.bind();
Drawable drawable = new Drawable(GLTextureFormat.RGBA, half, half); Drawable drawable = new Drawable(GLTextureFormat.RGBA, half, half);
drawable.fill(0, 0, half, half, 255, 255, 0, 255); drawable.fill(0, 0, half, half, 255, 255, 0, 255);
drawable.upload(base * 3, base * 3, base, base, base * 2, base * 2); drawable.upload(texture.id(), base * 3, base * 3, base, base, base * 2, base * 2);
drawable.close(); drawable.close();
} }
else if(T.key() == GLFW.GLFW_KEY_Z) { else if(T.key() == GLFW.GLFW_KEY_Z) {
@ -114,7 +115,7 @@ public class NewInputTest {
public Glyth bake(IGlythSheetInfo info) { public Glyth bake(IGlythSheetInfo info) {
int width = info.width(); int width = info.width();
int height = info.height(); int height = info.height();
info.upload(half - (width >> 1), half - (height >> 1)); info.upload(texture.id(), half - (width >> 1), half - (height >> 1));
System.out.println("Test2: "+width+", "+height); System.out.println("Test2: "+width+", "+height);
return null; return null;
} }
@ -127,7 +128,7 @@ public class NewInputTest {
public Glyth bake(IGlythSheetInfo info) { public Glyth bake(IGlythSheetInfo info) {
int width = info.width(); int width = info.width();
int height = info.height(); int height = info.height();
info.upload(half - (width >> 1), half - (height >> 1)); info.upload(texture.id(), half - (width >> 1), half - (height >> 1));
System.out.println("Test3: "+width+", "+height); System.out.println("Test3: "+width+", "+height);
return null; return null;
} }
@ -144,7 +145,7 @@ public class NewInputTest {
builder.pos(-0.5F, 0.5F, 0).tex(0F, 0F).rgba(-1).endVertex(); builder.pos(-0.5F, 0.5F, 0).tex(0F, 0F).rgba(-1).endVertex();
builder.pos(-0.5F, -0.5F, 0).tex(0F, 1F).rgba(-1).endVertex(); builder.pos(-0.5F, -0.5F, 0).tex(0F, 1F).rgba(-1).endVertex();
Font font = fonts.createFont(18.5F); Font font = fonts.createFont(18F, 4F);
TestModel model = new TestModel(builder.getBytes()); TestModel model = new TestModel(builder.getBytes());
TestModel[] guiModel = new TestModel[1]; TestModel[] guiModel = new TestModel[1];
List<GLDraw> draws = new ObjectArrayList<>(); List<GLDraw> draws = new ObjectArrayList<>();
@ -154,25 +155,24 @@ public class NewInputTest {
System.out.println("Testing: "+V.length+" bytes, "+K.size()); System.out.println("Testing: "+V.length+" bytes, "+K.size());
})); }));
GLStateTracker tracker = GLStateTracker.instance();
GL11.glClearColor(0.2F, 0.55F, 0.66F, 1F); GL11.glClearColor(0.2F, 0.55F, 0.66F, 1F);
while(!window.shouldClose()) { while(!window.shouldClose()) {
GLFW.glfwPollEvents(); GLFW.glfwPollEvents();
window.beginFrame(); window.beginFrame();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
shaderTest.bind(); // texture.bind();
shaderTest.get().texture.set(texture); // shaderTest.bind();
model.bindArray(); // model.bindArray();
GLStateTracker.drawArrays(GLMode.TRIANGLES.glValue(), 6); // GLStateTracker.drawArrays(GLMode.TRIANGLES.glValue(), 6);
model.unbindArray(); // model.unbindArray();
guiModel[0].bindArray(); guiModel[0].bindArray();
guiShader.bind(); guiShader.bind();
for(GLDraw draw : draws) { for(GLDraw draw : draws) {
// System.out.println("Draw: "+draw.texture()+", "+draw.startVertex()+", "+draw.vertexCount()); tracker.textures.bind(draw.texture());
guiShader.get().texture.set(draw.texture());
GLStateTracker.drawArrays(GLMode.TRIANGLES.glValue(), draw.startVertex(), draw.vertexCount()); GLStateTracker.drawArrays(GLMode.TRIANGLES.glValue(), draw.startVertex(), draw.vertexCount());
} }
guiModel[0].unbindArray(); guiModel[0].unbindArray();
window.handleInput(); window.handleInput();
window.finishFrame(); window.finishFrame();
try { Thread.sleep(100); } try { Thread.sleep(100); }
@ -216,6 +216,7 @@ public class NewInputTest {
int count = builder.size() - lastVertex; int count = builder.size() - lastVertex;
draws.add(new GLDraw(previousId, lastVertex, count)); draws.add(new GLDraw(previousId, lastVertex, count));
callbacks.accept(draws, builder.getBytes()); callbacks.accept(draws, builder.getBytes());
builder.close();
} }
} }
@ -227,6 +228,7 @@ public class NewInputTest {
public static class GuiShader extends SimpleShader { public static class GuiShader extends SimpleShader {
public TextureUniform texture = uniforms.addTexture("texture", 0); public TextureUniform texture = uniforms.addTexture("texture", 0);
public Matrix4fUniform proView = uniforms.addMat("proViewMatrix", new Matrix4f()); public Matrix4fUniform proView = uniforms.addMat("proViewMatrix", new Matrix4f());
public Matrix4fUniform model = uniforms.addMat("modelmatrix", new Matrix4f());
public GuiShader(IAssetProvider provider) { public GuiShader(IAssetProvider provider) {
super(provider, "gui_shader", AssetLocation.of("shader/testGui/vertex.vs"), AssetLocation.of("shader/testGui/fragment.fs"), "in_position", "in_tex", "in_color"); super(provider, "gui_shader", AssetLocation.of("shader/testGui/vertex.vs"), AssetLocation.of("shader/testGui/fragment.fs"), "in_position", "in_tex", "in_color");

View File

@ -63,13 +63,15 @@ public class Font {
float maxX = glyth.right() + x; float maxX = glyth.right() + x;
float maxY = glyth.bottom() + y; float maxY = glyth.bottom() + y;
// System.out.println("Test: MinX="+minX+", MinY="+minY+", MaxX="+maxX+", MaxY="+maxY+", MinU="+glyth.minU()+", MinV="+glyth.minV()+", MaxU="+glyth.maxU()+", MaxV="+glyth.maxV());
IVertexBuilder builder = buffer.builderForTexture(glyth.texture()); IVertexBuilder builder = buffer.builderForTexture(glyth.texture());
builder.pos(minX, minY, 0F).tex(glyth.minU(), glyth.maxV()).rgba(-1).endVertex(); builder.pos(minX, minY, 0F).tex(glyth.minU(), glyth.minV()).rgba(-1).endVertex();
builder.pos(maxX, minY, 0F).tex(glyth.maxU(), glyth.maxV()).rgba(-1).endVertex(); builder.pos(maxX, minY, 0F).tex(glyth.maxU(), glyth.minV()).rgba(-1).endVertex();
builder.pos(maxX, maxY, 0F).tex(glyth.maxU(), glyth.minV()).rgba(-1).endVertex(); builder.pos(maxX, maxY, 0F).tex(glyth.maxU(), glyth.maxV()).rgba(-1).endVertex();
builder.pos(maxX, maxY, 0F).tex(glyth.maxU(), glyth.minV()).rgba(-1).endVertex(); builder.pos(maxX, maxY, 0F).tex(glyth.maxU(), glyth.maxV()).rgba(-1).endVertex();
builder.pos(minX, maxY, 0F).tex(glyth.minU(), glyth.minV()).rgba(-1).endVertex(); builder.pos(minX, maxY, 0F).tex(glyth.minU(), glyth.maxV()).rgba(-1).endVertex();
builder.pos(minX, minY, 0F).tex(glyth.minU(), glyth.maxV()).rgba(-1).endVertex(); builder.pos(minX, minY, 0F).tex(glyth.minU(), glyth.minV()).rgba(-1).endVertex();
} }
x += data.advance(); x += data.advance();
i += Character.charCount(codepoint); i += Character.charCount(codepoint);

View File

@ -23,7 +23,7 @@ import speiger.src.coreengine.rendering.gui.font.providers.STBTrueTypeProvider;
import speiger.src.coreengine.utils.helpers.JsonUtil; import speiger.src.coreengine.utils.helpers.JsonUtil;
public class FontManager extends SteppedReloadableAsset<Map<AssetLocation, ObjectList<IFontProvider>>> { public class FontManager extends SteppedReloadableAsset<Map<AssetLocation, ObjectList<IFontProvider>>> {
private static final int TEXTURE_SIZE = 2048; private static final int TEXTURE_SIZE = 512;
private static final AssetFilter FILTER = AssetFilter.json("font"); private static final AssetFilter FILTER = AssetFilter.json("font");
Map<AssetLocation, FontGroup> fonts = Object2ObjectMap.builder().linkedMap(); Map<AssetLocation, FontGroup> fonts = Object2ObjectMap.builder().linkedMap();
Map<String, BiFunction<JsonObject, IAssetProvider, IFontProvider>> fontParsers = Object2ObjectMap.builder().map(); Map<String, BiFunction<JsonObject, IAssetProvider, IFontProvider>> fontParsers = Object2ObjectMap.builder().map();

View File

@ -1,8 +1,10 @@
package speiger.src.coreengine.rendering.gui.font; package speiger.src.coreengine.rendering.gui.font;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL45;
import speiger.src.coreengine.assets.base.IAssetProvider; import speiger.src.coreengine.assets.base.IAssetProvider;
import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.rendering.gui.font.glyth.Glyth; import speiger.src.coreengine.rendering.gui.font.glyth.Glyth;
import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo;
import speiger.src.coreengine.rendering.textures.base.BaseTexture; import speiger.src.coreengine.rendering.textures.base.BaseTexture;
@ -16,10 +18,10 @@ import speiger.src.coreengine.rendering.utils.values.textures.GLTextureValue;
public class FontTexture extends BaseTexture { public class FontTexture extends BaseTexture {
private static final TextureMetadata COLOR = TextureMetadata.builder() private static final TextureMetadata COLOR = TextureMetadata.builder()
.externalFormat(GLTextureFormat.RGBA).internalFormat(GLTextureFormat.RGBA).dataFormat(GLDataType.UNSIGNED_BYTE).mipmapping(false) .externalFormat(GLTextureFormat.RGBA).internalFormat(GLTextureFormat.RGBAI).dataFormat(GLDataType.UNSIGNED_BYTE).mipmapping(false)
.arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST) .arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST)
.arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build(); .arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build();
private static final TextureMetadata NO_COLOR = COLOR.copy().internalFormat(GLTextureFormat.LUMINANCE).externalFormat(GLTextureFormat.LUMINANCE).arguement(GLTextureParameter.SWIZZLE_RGBA, GLTextureValue.RED).build(); private static final TextureMetadata NO_COLOR = COLOR.copy().internalFormat(GLTextureFormat.LUMINANCE).externalFormat(GLTextureFormat.R).specialarguments(FontTexture::swizzle).build();
boolean color; boolean color;
int bounds; int bounds;
@ -33,36 +35,36 @@ public class FontTexture extends BaseTexture {
} }
public static ITextureComponentFormat formatByColor(boolean colored) { public static ITextureComponentFormat formatByColor(boolean colored) {
return colored ? GLTextureFormat.RGBA : GLTextureFormat.LUMINANCE; return colored ? GLTextureFormat.RGBA : GLTextureFormat.R;
} }
public static TextureMetadata metadataByColor(boolean colored) { public static TextureMetadata metadataByColor(boolean colored) {
return colored ? COLOR : NO_COLOR; return colored ? COLOR : NO_COLOR;
} }
private static void swizzle(int texture) {
GL45.glTextureParameterIiv(texture, GLTextureParameter.SWIZZLE_RGBA.glValue(), new int[]{GL11.GL_RED, GL11.GL_RED, GL11.GL_RED, GL11.GL_RED});
}
@Override @Override
public void load(IAssetProvider provider) { public void load(IAssetProvider provider) {
createTexture(); createTexture();
TextureMetadata metadata = metadataByColor(color); TextureMetadata metadata = metadataByColor(color);
metadata.applyArguments(textureType()); metadata.applyArguments(id());
long allocate = MemoryUtil.nmemAlloc(bounds * bounds * formatByColor(color).components()); GLFunctions.prepare2DImage(id(), 1, metadata, bounds, bounds);
ITextureComponentFormat format = formatByColor(color);
GLFunctions.upload2DImage(textureType(), 0, format, bounds, bounds, 0, format, GLDataType.UNSIGNED_BYTE, allocate);
// GLFunctions.upload2DImage(textureType(), 0, metadata, bounds, bounds, 0, allocate);
MemoryUtil.nmemFree(allocate);
} }
public Glyth build(IGlythSheetInfo info) { public Glyth build(IGlythSheetInfo info) {
if(color != info.isColored()) return null; if(color != info.isColored()) return null;
Slot result = slot.insert(info); Slot result = slot.insert(info);
if(result != null) { if(result != null) {
bind(); info.upload(id(), result.x, result.y);
info.upload(result.x, result.y); float minU = (float)(result.x) / (float)bounds;
float minU = (slot.x + 0.01F) / bounds; float maxU = (float)((result.x) + info.width()) / (float)bounds;
float maxU = (slot.x - 0.01F + info.width()) / bounds; float minV = (float)(result.y) / (float)bounds;
float minV = (slot.y + 0.01F) / bounds; float maxV = (float)((result.y) + info.height()) / (float)bounds;
float maxV = (slot.y - 0.01F + info.height()) / bounds; System.out.println("Baking: MinU="+minU+", MinV="+minV+", MaxU="+maxU+", MaxV="+maxV+", "+result.x+", "+result.y);
return new Glyth(id(), minU, minV, maxU, maxV); return new Glyth(id(), minU, minV, maxU, maxV, info.left(), info.right(), info.top(), info.bottom());
} }
return null; return null;
} }
@ -100,9 +102,17 @@ public class FontTexture extends BaseTexture {
} }
int dw = width - iw; int dw = width - iw;
int dh = height - ih; int dh = height - ih;
children = new Slot[dw > 0 && dh > 0 ? 3 : 2]; children = new Slot[2];
children[0] = new Slot(x, y, iw, ih); if(dw > dh) {
expandSlot(iw, ih, dw, dh); int offset = MathUtils.ceil(iw / 50F);
children[0] = new Slot(x, y, iw, height);
children[1] = new Slot(x + iw + offset, y, dw - offset, height);
}
else {
int offset = MathUtils.ceil(ih / 50F);
children[0] = new Slot(x, y, width, ih);
children[1] = new Slot(x, y + ih + offset, width, dh - offset);
}
return children[0].insert(info); return children[0].insert(info);
} }
for(int i = 0,m=children.length;i<m;i++) { for(int i = 0,m=children.length;i<m;i++) {
@ -111,22 +121,7 @@ public class FontTexture extends BaseTexture {
} }
return null; return null;
} }
private void expandSlot(int rw, int rh, int dw, int dh) {
if(dw > 0 && dh > 0) {
if(dw >= dh) {
children[1] = new Slot(x + rw, y, dw, rh);
children[2] = new Slot(x, y + rh, width, dh);
}
else {
children[1] = new Slot(x, y + rh, rw, dh);
children[2] = new Slot(x + rw, y, dw, height);
}
}
else if(dw == 0) children[1] = new Slot(x, y + rh, rw, dh);
else if(dh == 0) children[1] = new Slot(x + rw, y, dw, rh);
}
@Override @Override
public String toString() { public String toString() {
return "Slot[x="+x+", y="+y+", w="+width+", h="+height+"]"; return "Slot[x="+x+", y="+y+", w="+width+", h="+height+"]";

View File

@ -1,10 +1,10 @@
package speiger.src.coreengine.rendering.gui.font.glyth; package speiger.src.coreengine.rendering.gui.font.glyth;
public record Glyth(int texture, float minU, float minV, float maxU, float maxV, int left, int right, int top, int bottom) { public record Glyth(int texture, float minU, float minV, float maxU, float maxV, float left, float right, float top, float bottom) {
public static final Glyth EMPTY = new Glyth(); public static final Glyth EMPTY = new Glyth();
private Glyth() { private Glyth() {
this(-1, 0F, 0F, 0F, 0F, 0, 0, 0, 0); this(-1, 0F, 0F, 0F, 0F, 0F, 0F, 0F, 0F);
} }
public Glyth(int texture, float minU, float minV, float maxU, float maxV) { public Glyth(int texture, float minU, float minV, float maxU, float maxV) {

View File

@ -7,7 +7,7 @@ public interface IGlythSheetInfo {
public default float yOffset() { return 3F; } public default float yOffset() { return 3F; }
public float oversample(); public float oversample();
public boolean isColored(); public boolean isColored();
public void upload(int x, int y); public void upload(int texture, int x, int y);
public default float left() { return xOffset(); } public default float left() { return xOffset(); }
public default float right() { return xOffset() + width() / oversample(); } public default float right() { return xOffset() + width() / oversample(); }

View File

@ -41,10 +41,10 @@ public class MissingGlyth implements GlythData {
@Override @Override
public int height() { return height; } public int height() { return height; }
@Override @Override
public void upload(int x, int y) { public void upload(int texture, int x, int y) {
Drawable drawable = new Drawable(FontTexture.formatByColor(false), width, height); Drawable drawable = new Drawable(FontTexture.formatByColor(false), width, height);
draw(drawable); draw(drawable);
drawable.upload(x, y, 0, 0, width, height); drawable.upload(texture, x, y, 0, 0, width, height);
drawable.close(); drawable.close();
} }
@Override @Override
@ -58,7 +58,7 @@ public class MissingGlyth implements GlythData {
private void draw(Drawable texture) { private void draw(Drawable texture) {
texture.fill(0, 0, width, height, 255); texture.fill(0, 0, width, height, 255);
int offset = MathUtils.ceil(width / 7.5F); int offset = MathUtils.ceil(width / 5F);
texture.fill(offset, offset, width - offset * 2, height - offset * 2, 0); texture.fill(offset, offset, width - offset * 2, height - offset * 2, 0);
} }
} }

View File

@ -0,0 +1,23 @@
package speiger.src.coreengine.rendering.gui.font.providers;
import org.lwjgl.util.freetype.FreeType;
import speiger.src.coreengine.rendering.gui.font.glyth.GlythData;
public class FreeTypeProvider implements IFontProvider {
public void test() {
}
@Override
public GlythData glythData(int codepoint, int style, float size, float oversample) {
return null;
}
@Override
public void close() {
}
}

View File

@ -22,7 +22,6 @@ import speiger.src.coreengine.rendering.gui.font.glyth.GlythData;
import speiger.src.coreengine.rendering.gui.font.glyth.GlythData.EmptyGlythData; import speiger.src.coreengine.rendering.gui.font.glyth.GlythData.EmptyGlythData;
import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo;
import speiger.src.coreengine.rendering.textures.custom.Drawable; import speiger.src.coreengine.rendering.textures.custom.Drawable;
import speiger.src.coreengine.rendering.utils.GLStateTracker;
import speiger.src.coreengine.utils.helpers.JsonUtil; import speiger.src.coreengine.utils.helpers.JsonUtil;
public class STBTrueTypeProvider implements IFontProvider { public class STBTrueTypeProvider implements IFontProvider {
@ -203,11 +202,10 @@ public class STBTrueTypeProvider implements IFontProvider {
@Override @Override
public boolean isColored() { return false; } public boolean isColored() { return false; }
@Override @Override
public void upload(int x, int y) { public void upload(int texture, int x, int y) {
System.out.println("Testing: "+GLStateTracker.instance().textures.getTextureId(0));
Drawable drawable = new Drawable(FontTexture.formatByColor(false), width, height); Drawable drawable = new Drawable(FontTexture.formatByColor(false), width, height);
drawable.drawFont(owner.info, glyth, owner.xOff, owner.yOff, 0, 0, width, height, scale, scale); drawable.drawFont(owner.info, glyth, owner.xOff, owner.yOff, 0, 0, width, height, scale, scale);
drawable.upload(x, y, 0, 0, width, height); drawable.upload(texture, x, y, 0, 0, width, height);
drawable.close(); drawable.close();
} }
}); });

View File

@ -2,28 +2,20 @@ package speiger.src.coreengine.rendering.shader.uniform.base;
import org.lwjgl.opengl.GL41; import org.lwjgl.opengl.GL41;
import speiger.src.coreengine.rendering.shader.uniform.IAutoUniform;
import speiger.src.coreengine.rendering.shader.uniform.Uniform; import speiger.src.coreengine.rendering.shader.uniform.Uniform;
import speiger.src.coreengine.rendering.textures.base.ITexture;
import speiger.src.coreengine.rendering.utils.AllocationTracker; import speiger.src.coreengine.rendering.utils.AllocationTracker;
import speiger.src.coreengine.rendering.utils.GLStateTracker;
public class TextureUniform extends Uniform implements IAutoUniform { public class TextureUniform extends Uniform {
final int unit; int unit;
int value;
public TextureUniform(String name, int unit) { public TextureUniform(String name, int unit) {
super(name); super(name);
this.unit = unit; this.unit = unit;
} }
public TextureUniform set(ITexture texture) { public TextureUniform set(int unit) {
return set(texture.id()); if(this.unit != unit) {
} this.unit = unit;
public TextureUniform set(int id) {
if(this.value != id) {
this.value = id;
update(); update();
} }
return this; return this;
@ -31,12 +23,7 @@ public class TextureUniform extends Uniform implements IAutoUniform {
@Override @Override
protected void processChanges(int programId, int location) { protected void processChanges(int programId, int location) {
GL41.glProgramUniform1i(programId, location, value); GL41.glProgramUniform1i(programId, location, unit);
AllocationTracker.INSTANCE.addGPUBytes(4L); AllocationTracker.INSTANCE.addGPUBytes(4L);
} }
@Override
public void bind(int programId) {
GLStateTracker.instance().textures.bind(unit, value);
}
} }

View File

@ -26,6 +26,11 @@ public class VertexBuilder implements IVertexBuilder {
this.buffer = buffer; this.buffer = buffer;
} }
public void close() {
MemoryUtil.memFree(buffer);
buffer = null;
}
public VertexBuilder start(GLMode mode, VertexFormat format) { public VertexBuilder start(GLMode mode, VertexFormat format) {
this.mode = mode; this.mode = mode;
this.format = format; this.format = format;

View File

@ -16,8 +16,7 @@ public abstract class BaseTexture implements ITexture {
} }
protected void createTexture() { protected void createTexture() {
this.id = GLFunctions.createTexture(); this.id = GLFunctions.createTexture(textureType());
bind();
} }
protected IGLValue textureType() { protected IGLValue textureType() {

View File

@ -15,20 +15,20 @@ public class TextureMetadata {
int internalFormat; int internalFormat;
int externalFormat; int externalFormat;
int dataFormat; int dataFormat;
boolean isSTBData = true; boolean isSTBData = false;
boolean mipmapping = true; boolean mipmapping = false;
LongList arguments = new LongArrayList(); LongList arguments = new LongArrayList();
IntConsumer specialarguments; IntConsumer specialarguments;
private TextureMetadata() {} private TextureMetadata() {}
public void applyArguments(IGLValue textureType) { public void applyArguments(int texture) {
for(int i = 0,m=arguments.size();i<m;i++) { for(int i = 0,m=arguments.size();i<m;i++) {
long value = arguments.getLong(i); long value = arguments.getLong(i);
GLFunctions.textureParameter(textureType.glValue(), BitUtil.intKey(value), BitUtil.intValue(value)); GLFunctions.textureParameter(texture, BitUtil.intKey(value), BitUtil.intValue(value));
} }
if(specialarguments != null) { if(specialarguments != null) {
specialarguments.accept(textureType.glValue()); specialarguments.accept(texture);
} }
} }

View File

@ -9,9 +9,7 @@ import speiger.src.coreengine.math.misc.ColorSpaces;
import speiger.src.coreengine.rendering.utils.GLFunctions; import speiger.src.coreengine.rendering.utils.GLFunctions;
import speiger.src.coreengine.rendering.utils.GLStateTracker; import speiger.src.coreengine.rendering.utils.GLStateTracker;
import speiger.src.coreengine.rendering.utils.values.GLDataType; import speiger.src.coreengine.rendering.utils.values.GLDataType;
import speiger.src.coreengine.rendering.utils.values.IGLValue;
import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureComponentFormat; import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureComponentFormat;
import speiger.src.coreengine.rendering.utils.values.textures.GLTextureType;
public class Drawable implements IDrawable, AutoCloseable { public class Drawable implements IDrawable, AutoCloseable {
ITextureComponentFormat format; ITextureComponentFormat format;
@ -73,11 +71,11 @@ public class Drawable implements IDrawable, AutoCloseable {
STBTruetype.nstbtt_MakeGlyphBitmapSubpixel(info.address(), pixels + offset(targetX, targetY), width, height, width(), scaleX, scaleY, sourceX, sourceY, glyth); STBTruetype.nstbtt_MakeGlyphBitmapSubpixel(info.address(), pixels + offset(targetX, targetY), width, height, width(), scaleX, scaleY, sourceX, sourceY, glyth);
} }
public void upload(int targetX, int targetY, int sourceX, int sourceY, int width, int height) { public void upload(int texture, int targetX, int targetY, int sourceX, int sourceY, int width, int height) {
upload(GLTextureType.TEXTURE_2D, 0, targetX, targetY, sourceX, sourceY, width, height); upload(texture, 0, targetX, targetY, sourceX, sourceY, width, height);
} }
public void upload(IGLValue textureType, int level, int targetX, int targetY, int sourceX, int sourceY, int width, int height) { public void upload(int texture, int level, int targetX, int targetY, int sourceX, int sourceY, int width, int height) {
ensureValid(sourceX, sourceY); ensureValid(sourceX, sourceY);
ensureValid(sourceX + width, sourceY + height); ensureValid(sourceX + width, sourceY + height);
GLStateTracker tracker = GLStateTracker.instance(); GLStateTracker tracker = GLStateTracker.instance();
@ -85,7 +83,7 @@ public class Drawable implements IDrawable, AutoCloseable {
tracker.unpack_alignment.set(format.components()); tracker.unpack_alignment.set(format.components());
tracker.unpack_skip_pixel.set(sourceX); tracker.unpack_skip_pixel.set(sourceX);
tracker.unpack_skip_rows.set(sourceY); tracker.unpack_skip_rows.set(sourceY);
GLFunctions.upload2DSubImage(textureType, level, targetX, targetY, width, height, format, GLDataType.UNSIGNED_BYTE, pixels); GLFunctions.upload2DSubImage(texture, level, targetX, targetY, width, height, format, GLDataType.UNSIGNED_BYTE, pixels);
tracker.unpack_row_length.setDefault(); tracker.unpack_row_length.setDefault();
tracker.unpack_skip_pixel.setDefault(); tracker.unpack_skip_pixel.setDefault();
tracker.unpack_skip_rows.setDefault(); tracker.unpack_skip_rows.setDefault();

View File

@ -18,7 +18,7 @@ import speiger.src.coreengine.rendering.utils.values.textures.GLTextureParameter
import speiger.src.coreengine.rendering.utils.values.textures.GLTextureValue; import speiger.src.coreengine.rendering.utils.values.textures.GLTextureValue;
public class DynamicTexture extends BaseTexture implements IDynamicTexture { public class DynamicTexture extends BaseTexture implements IDynamicTexture {
public static final TextureMetadata DEFAULT_PARAMETERS = TextureMetadata.builder() public static final TextureMetadata DEFAULT_PARAMETERS = TextureMetadata.builder().internalFormat(GLTextureFormat.RGBAI).externalFormat(GLTextureFormat.RGBA).dataFormat(GLDataType.UNSIGNED_BYTE)
.arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST) .arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST)
.arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build(); .arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build();
IntSet dirtySections = new IntLinkedOpenHashSet(); IntSet dirtySections = new IntLinkedOpenHashSet();
@ -39,8 +39,8 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture {
@Override @Override
public void load(IAssetProvider provider) { public void load(IAssetProvider provider) {
createTexture(); createTexture();
metadata.applyArguments(textureType()); metadata.applyArguments(id());
GLFunctions.upload2DImage(textureType(), 0, GLTextureFormat.RGBA, width, height, 0, GLTextureFormat.RGBA, GLDataType.UNSIGNED_BYTE, data); GLFunctions.prepare2DImage(id(), 1, metadata, width, height);
dirtySections.clear(); dirtySections.clear();
} }
@ -64,9 +64,8 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture {
@Override @Override
public void process(boolean full) { public void process(boolean full) {
bind();
if(full) { if(full) {
GLFunctions.upload2DImage(textureType(), 0, GLTextureFormat.RGBA, width, height, 0, GLTextureFormat.RGBA, GLDataType.UNSIGNED_BYTE, data); GLFunctions.upload2DSubImage(id(), 0, 0, 0, width, height, GLTextureFormat.RGBA, GLDataType.UNSIGNED_BYTE, data);
dirtySections.clear(); dirtySections.clear();
return; return;
} }
@ -85,7 +84,7 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture {
protected void uploadPixels(GLStateTracker tracker, int x, int y) { protected void uploadPixels(GLStateTracker tracker, int x, int y) {
tracker.unpack_skip_pixel.set(x); tracker.unpack_skip_pixel.set(x);
tracker.unpack_skip_rows.set(y); tracker.unpack_skip_rows.set(y);
GLFunctions.upload2DSubImage(textureType(), 0, x, y, 16, 16, GLTextureFormat.RGBA, GLDataType.UNSIGNED_BYTE, data); GLFunctions.upload2DSubImage(id(), 0, x, y, 16, 16, GLTextureFormat.RGBA, GLDataType.UNSIGNED_BYTE, data);
} }
protected long offset(int x, int y) { protected long offset(int x, int y) {

View File

@ -48,8 +48,8 @@ public class DirectTexture extends BaseTexture {
private void loadTexture() { private void loadTexture() {
createTexture(); createTexture();
metadata.applyArguments(textureType()); metadata.applyArguments(id());
GLFunctions.upload2DImage(textureType(), 0, metadata, width, height, 0, imageData); GLFunctions.upload2DImage(id(), 0, metadata, width, height, 0, imageData);
if(metadata.generateMipMappings()) GLFunctions.generateMipMaps(textureType()); if(metadata.generateMipMappings()) GLFunctions.generateMipMaps(textureType());
} }
} }

View File

@ -21,7 +21,7 @@ import speiger.src.coreengine.rendering.utils.values.textures.GLTextureValue;
public class SimpleTexture extends BaseTexture { public class SimpleTexture extends BaseTexture {
public static final TextureMetadata DEFAULT_DATA = TextureMetadata.builder() public static final TextureMetadata DEFAULT_DATA = TextureMetadata.builder()
.externalFormat(GLTextureFormat.RGBA).internalFormat(GLTextureFormat.RGBA).dataFormat(GLDataType.UNSIGNED_BYTE).mipmapping(false) .externalFormat(GLTextureFormat.RGBA).internalFormat(GLTextureFormat.RGBAI).dataFormat(GLDataType.UNSIGNED_BYTE).mipmapping(false)
.arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST) .arguement(GLTextureParameter.MIN_FILTER, GLTextureValue.NEAREST).arguement(GLTextureParameter.MAG_FILTER, GLTextureValue.NEAREST)
.arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build(); .arguement(GLTextureParameter.WRAP_S, GLTextureValue.CLAMP_TO_EDGE).arguement(GLTextureParameter.WRAP_T, GLTextureValue.CLAMP_TO_EDGE).build();
AssetLocation location; AssetLocation location;
@ -58,8 +58,8 @@ public class SimpleTexture extends BaseTexture {
} }
if(address == 0L) return; if(address == 0L) return;
createTexture(); createTexture();
metadata.applyArguments(textureType()); metadata.applyArguments(id());
GLFunctions.upload2DImage(textureType(), 0, metadata, width, height, 0, address); GLFunctions.upload2DImage(id(), 0, metadata, width, height, 0, address);
if(metadata.generateMipMappings()) GLFunctions.generateMipMaps(textureType()); if(metadata.generateMipMappings()) GLFunctions.generateMipMaps(textureType());
STBImage.nstbi_image_free(address); STBImage.nstbi_image_free(address);
} }

View File

@ -1,8 +1,8 @@
package speiger.src.coreengine.rendering.utils; package speiger.src.coreengine.rendering.utils;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GL45;
import speiger.src.coreengine.rendering.textures.base.TextureMetadata; import speiger.src.coreengine.rendering.textures.base.TextureMetadata;
import speiger.src.coreengine.rendering.utils.values.IGLValue; import speiger.src.coreengine.rendering.utils.values.IGLValue;
@ -12,46 +12,51 @@ import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureParameter;
public class GLFunctions { public class GLFunctions {
public static int createTexture() { public static int createTexture(IGLValue textureType) {
return GL11.glGenTextures(); return GL45.glCreateTextures(textureType.glValue());
} }
public static void deleteTextures(int... id) { public static void deleteTextures(int... id) {
GL11.glDeleteTextures(id); GL11.glDeleteTextures(id);
} }
public static void textureParameter(IGLValue type, ITextureParameter parameter, IGLValue value) { public static void textureParameter(int texture, ITextureParameter parameter, IGLValue value) {
textureParameter(type, parameter, value.glValue()); textureParameter(texture, parameter, value.glValue());
} }
public static void textureParameter(IGLValue type, ITextureParameter parameter, int value) { public static void textureParameter(int texture, ITextureParameter parameter, int value) {
if(!parameter.isValid(value)) throw new IllegalArgumentException("Value ["+value+"] isn't supported for GL Function ["+parameter.glValue()+"], only int functions are supported with this"); if(!parameter.isValid(value)) throw new IllegalArgumentException("Value ["+value+"] isn't supported for GL Function ["+parameter.glValue()+"], only int functions are supported with this");
textureParameter(type.glValue(), parameter.glValue(), value); textureParameter(texture, parameter.glValue(), value);
} }
public static void textureParameter(int type, int parameter, int value) { public static void textureParameter(int texture, int parameter, int value) {
GL15.glTexParameteri(type, parameter, value); GL45.glTextureParameteri(texture, parameter, value);
} }
public static void upload2DImage(IGLValue target, int mipmaps, TextureMetadata metadata, int width, int height, int border, long pixels) { public static void prepare2DImage(int texture, int levels, TextureMetadata metadata, int width, int height) {
upload2DImage(target.glValue(), mipmaps, metadata.getInternalFormat(), width, width, border, metadata.getExternalFormat(), metadata.getDataFormat(), pixels); GL45.glTextureStorage2D(texture, levels, metadata.getInternalFormat(), width, height);
} }
public static void upload2DImage(IGLValue target, int mipmaps, ITextureFormatType internalFormat, int width, int height, int border, ITextureFormatType externalFormat, IGLDataType data, long pixels) { public static void upload2DImage(int texture, int mipmaps, TextureMetadata metadata, int width, int height, int border, long pixels) {
upload2DImage(target.glValue(), mipmaps, internalFormat.glValue(), width, height, border, externalFormat.glValue(), data.glValue(), pixels); upload2DImage(texture, mipmaps, metadata.getInternalFormat(), width, width, border, metadata.getExternalFormat(), metadata.getDataFormat(), pixels);
} }
public static void upload2DImage(int target, int mipmaps, int internalFormat, int width, int height, int border, int externalFormat, int dataFormat, long pixels) { public static void upload2DImage(int texture, int mipmaps, ITextureFormatType internalFormat, int width, int height, int border, ITextureFormatType externalFormat, IGLDataType data, long pixels) {
GL11.glTexImage2D(target, mipmaps, internalFormat, width, height, border, externalFormat, dataFormat, pixels); upload2DImage(texture, mipmaps, internalFormat.glValue(), width, height, border, externalFormat.glValue(), data.glValue(), pixels);
} }
public static void upload2DSubImage(IGLValue target, int level, int xoffset, int yoffset, int width, int height, ITextureFormatType format, IGLDataType type, long pixels) { public static void upload2DImage(int texture, int mipmaps, int internalFormat, int width, int height, int border, int externalFormat, int dataFormat, long pixels) {
upload2DSubImage(target.glValue(), level, xoffset, yoffset, width, height, format.glValue(), type.glValue(), pixels); GL45.glTextureStorage2D(texture, mipmaps+1, internalFormat, width, height);
GL45.glTextureSubImage2D(texture, mipmaps, 0, 0, width, height, externalFormat, dataFormat, pixels);
} }
public static void upload2DSubImage(int target, int level, int xOffset, int yOffset, int width, int height, int format, int type, long pixels) { public static void upload2DSubImage(int texture, int level, int xoffset, int yoffset, int width, int height, ITextureFormatType format, IGLDataType type, long pixels) {
GL15.nglTexSubImage2D(target, level, xOffset, yOffset, width, height, format, type, pixels); // if(format.internal()) throw new IllegalArgumentException("Format ["+format.glValue()+"] is internal");
upload2DSubImage(texture, level, xoffset, yoffset, width, height, format.glValue(), type.glValue(), pixels);
}
public static void upload2DSubImage(int texture, int level, int xOffset, int yOffset, int width, int height, int format, int type, long pixels) {
GL45.glTextureSubImage2D(texture, level, xOffset, yOffset, width, height, format, type, pixels);
} }
public static void generateMipMaps(IGLValue type) { public static void generateMipMaps(IGLValue type) {

View File

@ -8,6 +8,10 @@ import org.lwjgl.opengl.GL45;
public class TextureState implements IGLState { public class TextureState implements IGLState {
int[] textures = new int[GL20.GL_TEXTURE31 - GL20.GL_TEXTURE0]; int[] textures = new int[GL20.GL_TEXTURE31 - GL20.GL_TEXTURE0];
public TextureState bind(int texture) {
return bind(0, texture);
}
public TextureState bind(int unit, int texture) { public TextureState bind(int unit, int texture) {
if(textures[unit] != texture) { if(textures[unit] != texture) {
this.textures[unit] = texture; this.textures[unit] = texture;

View File

@ -6,14 +6,16 @@ import org.lwjgl.opengl.GL30;
import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureComponentFormat; import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureComponentFormat;
public enum GLTextureFormat implements ITextureComponentFormat { public enum GLTextureFormat implements ITextureComponentFormat {
R(GL11.GL_RED, 1, 0, -1, -1, -1, true), R(GL11.GL_RED, 1, 0, -1, -1, -1, false),
RG(GL30.GL_RG, 2, 0, 1, -1, -1, true), RG(GL30.GL_RG, 2, 0, 1, -1, -1, false),
RGB(GL11.GL_RGB, 3, 0, 1, 2, -1, true), RGB(GL11.GL_RGB, 3, 0, 1, 2, -1, false),
RGBA(GL11.GL_RGBA, 4, 0, 1, 2, 3, true), RGBI(GL11.GL_RGB8, 3, 0, 1, 2, -1, true),
DEPTH(GL11.GL_DEPTH_COMPONENT, 1, 0, -1, -1, -1, true), RGBA(GL11.GL_RGBA, 4, 0, 1, 2, 3, false),
DEPTH_STENCIL(GL30.GL_DEPTH_STENCIL, 2, 0, 1, -1, -1, true), RGBAI(GL11.GL_RGBA8, 4, 0, 1, 2, 3, true),
LUMINANCE(GL11.GL_LUMINANCE, 1, 0, 0, 0, 0, true), DEPTH(GL11.GL_DEPTH_COMPONENT, 1, 0, -1, -1, -1, false),
LUMINANCE_ALPHA(GL11.GL_LUMINANCE_ALPHA, 2, 0, -1, -1, 1, true); DEPTH_STENCIL(GL30.GL_DEPTH_STENCIL, 2, 0, 1, -1, -1, false),
LUMINANCE(GL30.GL_R8, 1, 0, 0, 0, 0, true),
LUMINANCE_ALPHA(GL11.GL_LUMINANCE8_ALPHA8, 2, 0, -1, -1, 1, true);
int glMode; int glMode;
boolean internal; boolean internal;
@ -52,8 +54,8 @@ public enum GLTextureFormat implements ITextureComponentFormat {
public static int stbComponents(int glValue) { public static int stbComponents(int glValue) {
return switch(glValue) { return switch(glValue) {
case GL11.GL_LUMINANCE -> 1; case GL30.GL_R8, GL11.GL_RED -> 1;
case GL11.GL_LUMINANCE_ALPHA -> 2; case GL11.GL_LUMINANCE8_ALPHA8, GL30.GL_RG -> 2;
case GL11.GL_RGB -> 3; case GL11.GL_RGB -> 3;
case GL11.GL_RGBA -> 4; case GL11.GL_RGBA -> 4;
default -> 0; default -> 0;

View File

@ -2,8 +2,7 @@
"type": "stb-ttf", "type": "stb-ttf",
"regular": { "regular": {
"file": "font/roboto/Roboto-Medium.ttf", "file": "font/roboto/Roboto-Medium.ttf",
"size": 18.5, "oversample": 1,
"oversample": 3,
"shadowOffset": 1, "shadowOffset": 1,
"skip": "", "skip": "",
"offset": { "x": 0, "y": 0 } "offset": { "x": 0, "y": 0 }

View File

@ -8,6 +8,7 @@ out vec4 pass_color;
out vec2 pass_tex; out vec2 pass_tex;
uniform mat4 proViewMatrix; uniform mat4 proViewMatrix;
uniform mat4 modelmatrix;
void main() void main()
{ {

View File

@ -1,11 +1,11 @@
#version 330 #version 420
in vec4 pass_color; in vec4 pass_color;
in vec2 pass_tex; in vec2 pass_tex;
out vec4 frag_color; out vec4 frag_color;
uniform sampler2D texture; layout(binding = 0) uniform sampler2D texture;
void main() void main()
{ {

View File

@ -1,4 +1,4 @@
#version 330 #version 420
layout(location = 0) in vec3 in_position; layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_tex; layout(location = 1) in vec2 in_tex;