From 7cedfaf9bdaa935ea544114cd634642d384e6b0c Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 20 Apr 2024 21:34:56 +0200 Subject: [PATCH] More Reworks --- .../application/ApplicationExecutor.java | 3 +- .../rendering/gui/GuiComponent.java | 6 +- .../coreengine/rendering/gui/GuiManager.java | 14 ++- .../gui/components/PieComponent.java | 4 +- .../rendering/input/window/Window.java | 4 +- .../models/buffers/IndirectArrayBuffer.java | 2 +- .../models/buffers/VertexBuffer.java | 42 +++---- .../models/frameBuffer/FrameBuffer.java | 4 +- .../coreengine/rendering/shader/Shader.java | 4 +- .../rendering/shader/ShaderProgram.java | 4 +- .../rendering/shader/uniform/Uniform.java | 2 +- .../shader/uniform/UniformManager.java | 4 +- .../shader/uniform/base/TextureUniform.java | 2 +- .../uniforms/UniformBufferBlock.java | 1 - .../commands/DrawArraysIndirectCommand.java | 16 ++- .../commands/DrawElementsIndirectCommand.java | 15 ++- .../rendering/textures/base/BaseTexture.java | 14 +-- .../textures/custom/DynamicTexture.java | 53 ++++++-- .../textures/custom/IDynamicTexture.java | 3 + .../texturesOld/base/TextureManager.java | 2 +- .../rendering/utils/AllocationTracker.java | 8 ++ .../rendering/utils/GLFunctions.java | 21 ++++ .../rendering/utils/GLStateTracker.java | 114 ++++++++++-------- .../rendering/utils/states/PixelState.java | 41 +++++++ .../utils/collections/CollectionUtils.java | 3 + .../utils/counters/averager/Averager.java | 2 +- .../coreengine/utils/eventbus/EventBus.java | 29 ++--- .../io/dataTag/parsing/DataTagIOBuilder.java | 1 + 28 files changed, 271 insertions(+), 147 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/utils/GLFunctions.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/utils/states/PixelState.java diff --git a/src/main/java/speiger/src/coreengine/application/ApplicationExecutor.java b/src/main/java/speiger/src/coreengine/application/ApplicationExecutor.java index c1b1fdd..cb88f52 100644 --- a/src/main/java/speiger/src/coreengine/application/ApplicationExecutor.java +++ b/src/main/java/speiger/src/coreengine/application/ApplicationExecutor.java @@ -83,7 +83,7 @@ public class ApplicationExecutor boolean end = timer.update(); stamp.stop(); AllocationTracker.INSTANCE.update(); - GLStateTracker.onFrameEnded(); + GLStateTracker.instance().onFrameEnded(); gpu().next("Window").start("V-Sync"); window.finishFrame(); if(window.isCPULimited()) sync.sync(fps.getTargetTicks()); @@ -126,6 +126,7 @@ public class ApplicationExecutor builder.append(fps.getTicks()).append(":").append(ups.getTicks()); owner.addExtraTickRates(T -> builder.append(":").append(T)); builder.append(",").append(getMemoryUsage()); + builder.append(",").append("OffHeap: "+(AllocationTracker.INSTANCE.getOffHeapMemory() >> 20)+"MB"); builder.append(",").append("CPU-A: "+IOUtils.convertBytes(AllocationTracker.INSTANCE.getCPUAllocatedBytes())); builder.append(",").append("GPU-A: "+IOUtils.convertBytes(AllocationTracker.INSTANCE.getGPUAllocatedBytes())); builder.append(",").append(TextUtil.convertTime(timer.getUsage(), "Client:", FORMATTER)); diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/GuiComponent.java b/src/main/java/speiger/src/coreengine/rendering/gui/GuiComponent.java index 351efe5..f6a510e 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/GuiComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/GuiComponent.java @@ -1071,7 +1071,7 @@ public abstract class GuiComponent extends FlagHolder int bottom = y + height; Window window = owner.getWindow(); Vec2d vec = owner.getUIManager().res.getScaleVec(); - GLStateTracker.TESTER.enableScissors((int)(x * vec.x()), (int)(window.getHeight() - bottom * vec.y()), (int)(width * vec.x()), (int)(height * vec.y())); + GLStateTracker.instance().scissors.enableScissors((int)(x * vec.x()), (int)(window.getHeight() - bottom * vec.y()), (int)(width * vec.x()), (int)(height * vec.y())); } protected final boolean isInScissors(Plane box) @@ -1094,13 +1094,13 @@ public abstract class GuiComponent extends FlagHolder int bottom = y + height; Window window = owner.getWindow(); Vec2d vec = owner.getUIManager().res.getScaleVec(); - return GLStateTracker.TESTER.isInScissors((int)(x * vec.x()), (int)(window.getHeight() - bottom * vec.y()), (int)(width * vec.x()), (int)(height * vec.y())); + return GLStateTracker.instance().scissors.isInScissors((int)(x * vec.x()), (int)(window.getHeight() - bottom * vec.y()), (int)(width * vec.x()), (int)(height * vec.y())); } public final void disableScissors() { getRenderer().flush(); - GLStateTracker.TESTER.disableScissors(); + GLStateTracker.instance().scissors.disableScissors(); } class KeyBindAction implements IKeyComponent diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/GuiManager.java b/src/main/java/speiger/src/coreengine/rendering/gui/GuiManager.java index 0d359f5..ed45fc2 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/GuiManager.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/GuiManager.java @@ -158,19 +158,21 @@ public abstract class GuiManager implements IWindowListener protected Vec2i start() { GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT); - GLStateTracker.DEBTH_TEST.disable(); - GLStateTracker.CULL_FACE.disable(); - GLStateTracker.BLEND.setFunction(GLBlendFactor.SRC_ALPHA, GLBlendFactor.ONE_MINUS_SRC_ALPHA).enable(); + GLStateTracker instance = GLStateTracker.instance(); + instance.debth_test.disable(); + instance.cull_face.disable(); + instance.blend.setFunction(GLBlendFactor.SRC_ALPHA, GLBlendFactor.ONE_MINUS_SRC_ALPHA).enable(); renderer.beginFrame(); return res.getScaledMouse(); } protected void stop() { - GLStateTracker.DEBTH_TEST.enable(); + GLStateTracker instance = GLStateTracker.instance(); + instance.debth_test.enable(); renderer.endFrame(); - GLStateTracker.BLEND.disable(); - GLStateTracker.CULL_FACE.disable(); + instance.blend.disable(); + instance.cull_face.disable(); ShaderTracker.INSTANCE.stopShader(); } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/components/PieComponent.java b/src/main/java/speiger/src/coreengine/rendering/gui/components/PieComponent.java index 016eb41..9b5ff2c 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/components/PieComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/components/PieComponent.java @@ -79,9 +79,9 @@ public class PieComponent extends GuiComponent float centerX = getBox().getCenterX(); float centerY = getBox().getCenterY(); getRenderer().setBrightness(getActiveBrightness()).translate(centerX, centerY).flush(); - GLStateTracker.DEBTH_TEST.enable(); + GLStateTracker.instance().debth_test.enable(); getRenderer().drawBuffers(buffer, 0F, 0F).flush(); - GLStateTracker.DEBTH_TEST.disable(); + GLStateTracker.instance().debth_test.disable(); getRenderer().translate(-centerX, -centerY).setBrightness(1F); return true; } diff --git a/src/main/java/speiger/src/coreengine/rendering/input/window/Window.java b/src/main/java/speiger/src/coreengine/rendering/input/window/Window.java index 5ca54ca..9d12aa5 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/window/Window.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/window/Window.java @@ -121,12 +121,12 @@ public class Window for(int i = 0,m=callbacks.size();i> data) { if(data.size() > 0) { long totalInjected = 0; - ByteBuffer buffer = GL15.glMapBuffer(type.glValue(), GL15.GL_WRITE_ONLY); + ByteBuffer buffer = GL45.glMapNamedBuffer(id, GL15.GL_WRITE_ONLY); for(int i = 0,m = data.size();i < m;i++) { IntObjectPair entry = data.get(i); buffer.position(entry.getIntKey() + offset); @@ -181,7 +181,7 @@ public class VertexBuffer { totalInjected += entry.getValue().length; } buffer.flip(); - if(!GL15.glUnmapBuffer(type.glValue())) GameLog.info("Memory Corruption?"); + if(!GL45.glUnmapNamedBuffer(id)) GameLog.info("Memory Corruption?"); AllocationTracker.INSTANCE.addGPUBytes(totalInjected); } return this; @@ -189,12 +189,12 @@ public class VertexBuffer { public VertexBuffer grow(int newSize) { if(size >= newSize) throw new IllegalArgumentException("New Size ["+newSize+"] is smaller then the buffer ["+size+"]"); - ByteBuffer buffer = GL15.glMapBuffer(type.glValue(), GL15.GL_READ_ONLY); + ByteBuffer buffer = GL45.glMapNamedBuffer(id, GL15.GL_READ_ONLY); if(size != buffer.remaining()) throw new IllegalStateException("Grow Function found inconsisten data: Found=["+buffer.remaining()+"], Expected=["+size+"]"); ByteBuffer newBuff = MemoryUtil.memAlloc(newSize); MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), MemoryUtil.memAddress(newBuff), size); newBuff.position(newSize).flip(); - GL15.glUnmapBuffer(type.glValue()); + GL45.glUnmapNamedBuffer(id); AllocationTracker.INSTANCE.addGPUBytes(size); set(MemoryUtil.memAddress(newBuff), newSize); MemoryUtil.memFree(newBuff); @@ -203,12 +203,12 @@ public class VertexBuffer { public VertexBuffer shrink(int newSize) { if(size <= newSize) throw new IllegalArgumentException("New Size ["+newSize+"] is bigger then the buffer ["+size+"]"); - ByteBuffer buffer = GL15.glMapBuffer(type.glValue(), GL15.GL_READ_WRITE); + ByteBuffer buffer = GL45.glMapNamedBuffer(id, GL15.GL_READ_WRITE); if(size != buffer.remaining()) throw new IllegalStateException("Grow Function found inconsisten data: Found=["+buffer.remaining()+"], Expected=["+size+"]"); ByteBuffer newBuff = MemoryUtil.memAlloc(newSize); MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), MemoryUtil.memAddress(newBuff), newSize); newBuff.position(newSize).flip(); - GL15.glUnmapBuffer(type.glValue()); + GL45.glUnmapNamedBuffer(id); AllocationTracker.INSTANCE.addGPUBytes(size); set(MemoryUtil.memAddress(newBuff), newSize); MemoryUtil.memFree(newBuff); @@ -218,7 +218,7 @@ public class VertexBuffer { public VertexBuffer shift(int source, int dest, int length) { if(source < 0 || source + length >= size) throw new ArrayIndexOutOfBoundsException(); if(dest < 0 || dest + length >= size) throw new ArrayIndexOutOfBoundsException(); - GL32.glCopyBufferSubData(id, id, source, dest, length); + GL45.glCopyNamedBufferSubData(id, id, source, dest, length); AllocationTracker.INSTANCE.addGPUBytes(length*2); return this; } diff --git a/src/main/java/speiger/src/coreengine/rendering/models/frameBuffer/FrameBuffer.java b/src/main/java/speiger/src/coreengine/rendering/models/frameBuffer/FrameBuffer.java index b4b7f51..a86d162 100644 --- a/src/main/java/speiger/src/coreengine/rendering/models/frameBuffer/FrameBuffer.java +++ b/src/main/java/speiger/src/coreengine/rendering/models/frameBuffer/FrameBuffer.java @@ -164,7 +164,7 @@ public class FrameBuffer public FrameBuffer bind() { GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, id); - GLStateTracker.VIEW_PORT.push(0, 0, width, height); + GLStateTracker.instance().viewPort.push(0, 0, width, height); return this; } @@ -183,7 +183,7 @@ public class FrameBuffer public FrameBuffer unbind() { GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); - GLStateTracker.VIEW_PORT.pop(); + GLStateTracker.instance().viewPort.pop(); return this; } diff --git a/src/main/java/speiger/src/coreengine/rendering/shader/Shader.java b/src/main/java/speiger/src/coreengine/rendering/shader/Shader.java index c10382c..996b305 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shader/Shader.java +++ b/src/main/java/speiger/src/coreengine/rendering/shader/Shader.java @@ -20,7 +20,7 @@ public class Shader implements Supplier { public static Shader createAndRegister(Function provider) { Shader shader = new Shader<>(provider); - GLStateTracker.SHADERS.register(shader); + GLStateTracker.instance().shaders.register(shader); return shader; } @@ -42,7 +42,7 @@ public class Shader implements Supplier { public void removeProgram() { remove(); - GLStateTracker.SHADERS.remove(this); + GLStateTracker.instance().shaders.remove(this); } @Override diff --git a/src/main/java/speiger/src/coreengine/rendering/shader/ShaderProgram.java b/src/main/java/speiger/src/coreengine/rendering/shader/ShaderProgram.java index 4cb3b8b..e72c3a2 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shader/ShaderProgram.java +++ b/src/main/java/speiger/src/coreengine/rendering/shader/ShaderProgram.java @@ -26,12 +26,12 @@ public abstract class ShaderProgram { private int id; public boolean isValid() { return id != 0; } - public boolean isActive() { return GLStateTracker.SHADERS.isShaderActive(this); } + public boolean isActive() { return GLStateTracker.instance().shaders.isShaderActive(this); } public int id() { return id; } public UniformManager getUniforms() { return uniforms; } public void bind() { - GLStateTracker.SHADERS.bind(this); + GLStateTracker.instance().shaders.bind(this); uniforms.bind(); } diff --git a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/Uniform.java b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/Uniform.java index a364649..51a3005 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/Uniform.java +++ b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/Uniform.java @@ -46,7 +46,7 @@ public abstract class Uniform implements IUniform { protected void update() { positions.forEach(this::processChanges); if(this instanceof IAutoUniform auto) { - int id = GLStateTracker.SHADERS.getActiveShader(); + int id = GLStateTracker.instance().shaders.getActiveShader(); if(contains(id)) auto.bind(id); } } diff --git a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/UniformManager.java b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/UniformManager.java index 2cb89b2..b2b38bb 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/UniformManager.java +++ b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/UniformManager.java @@ -29,11 +29,11 @@ public class UniformManager { } public T addGlobalUniform(String name) { - return addUniform(GLStateTracker.UNIFORMS.get(name)); + return addUniform(GLStateTracker.instance().uniforms.get(name)); } public T addGlobalUniform(String name, T defaultValue) { - return addUniform(GLStateTracker.UNIFORMS.getOrDefault(name, defaultValue)); + return addUniform(GLStateTracker.instance().uniforms.getOrDefault(name, defaultValue)); } public BoolUniform addBool(String name, boolean defaultValue) { diff --git a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/base/TextureUniform.java b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/base/TextureUniform.java index 2ede1e9..df51f35 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shader/uniform/base/TextureUniform.java +++ b/src/main/java/speiger/src/coreengine/rendering/shader/uniform/base/TextureUniform.java @@ -32,6 +32,6 @@ public class TextureUniform extends Uniform implements IAutoUniform { @Override public void bind(int programId) { - GLStateTracker.TEXTURES.bind(unit, value); + GLStateTracker.instance().textures.bind(unit, value); } } diff --git a/src/main/java/speiger/src/coreengine/rendering/shaderOld/uniforms/UniformBufferBlock.java b/src/main/java/speiger/src/coreengine/rendering/shaderOld/uniforms/UniformBufferBlock.java index b7912db..dd7b2eb 100644 --- a/src/main/java/speiger/src/coreengine/rendering/shaderOld/uniforms/UniformBufferBlock.java +++ b/src/main/java/speiger/src/coreengine/rendering/shaderOld/uniforms/UniformBufferBlock.java @@ -2,7 +2,6 @@ package speiger.src.coreengine.rendering.shaderOld.uniforms; import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL31; -import org.lwjgl.opengl.GL46; import speiger.src.coreengine.rendering.models.buffers.UniformBuffer; import speiger.src.coreengine.rendering.shaderOld.ShaderProgram; diff --git a/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawArraysIndirectCommand.java b/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawArraysIndirectCommand.java index 52b1006..a837777 100644 --- a/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawArraysIndirectCommand.java +++ b/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawArraysIndirectCommand.java @@ -8,14 +8,10 @@ public class DrawArraysIndirectCommand implements IndirectCommand { int first; int baseInstance; - public DrawArraysIndirectCommand() { - } + public DrawArraysIndirectCommand() {} public DrawArraysIndirectCommand(int count, int primCount, int first, int baseInstance) { - this.count = count; - this.primCount = primCount; - this.first = first; - this.baseInstance = baseInstance; + set(count, primCount, first, baseInstance); } @Override @@ -38,6 +34,14 @@ public class DrawArraysIndirectCommand implements IndirectCommand { public int getFirst() { return first; } public int getBaseInstance() { return baseInstance; } + public DrawArraysIndirectCommand set(int count, int primCount, int first, int baseInstance) { + this.count = count; + this.primCount = primCount; + this.first = first; + this.baseInstance = baseInstance; + return this; + } + public DrawArraysIndirectCommand setCount(int count) { this.count = count; return this; diff --git a/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawElementsIndirectCommand.java b/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawElementsIndirectCommand.java index 65b804d..f7bc96d 100644 --- a/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawElementsIndirectCommand.java +++ b/src/main/java/speiger/src/coreengine/rendering/tesselation/commands/DrawElementsIndirectCommand.java @@ -13,11 +13,7 @@ public class DrawElementsIndirectCommand implements IndirectCommand { } public DrawElementsIndirectCommand(int count, int primCount, int firstIndex, int baseVertex, int baseInstance) { - this.count = count; - this.primCount = primCount; - this.firstIndex = firstIndex; - this.baseVertex = baseVertex; - this.baseInstance = baseInstance; + set(count, primCount, firstIndex, baseVertex, baseInstance); } @Override @@ -42,6 +38,15 @@ public class DrawElementsIndirectCommand implements IndirectCommand { public int getBaseVertex() { return baseVertex; } public int getBaseInstance() { return baseInstance; } + public DrawElementsIndirectCommand set(int count, int primCount, int firstIndex, int baseVertex, int baseInstance) { + this.count = count; + this.primCount = primCount; + this.firstIndex = firstIndex; + this.baseVertex = baseVertex; + this.baseInstance = baseInstance; + return this; + } + public DrawElementsIndirectCommand setCount(int count) { this.count = count; return this; diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java b/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java index bb8c5e9..82f7349 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java @@ -1,8 +1,6 @@ package speiger.src.coreengine.rendering.textures.base; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL46; - +import speiger.src.coreengine.rendering.utils.GLFunctions; import speiger.src.coreengine.rendering.utils.GLStateTracker; import speiger.src.coreengine.rendering.utils.values.IGLValue; import speiger.src.coreengine.rendering.utils.values.textures.GLTextureType; @@ -20,7 +18,7 @@ public abstract class BaseTexture implements ITexture { } protected void createTexture() { - this.id = GL46.glCreateTextures(textureType().glValue()); + this.id = GLFunctions.createTexture(textureType()); } protected IGLValue textureType() { @@ -29,12 +27,12 @@ public abstract class BaseTexture implements ITexture { protected void removeTexture() { if(id == 0) return; - GL11.glDeleteTextures(id); + GLFunctions.deleteTextures(id); id = 0; } protected void track() { - GLStateTracker.TEXTURE_TRACKER.registerTexture(this); + GLStateTracker.instance().texture_tracker.registerTexture(this); } @Override @@ -44,14 +42,14 @@ public abstract class BaseTexture implements ITexture { @Override public void bind(int unit) { - GLStateTracker.TEXTURES.bind(unit, id); + GLStateTracker.instance().textures.bind(unit, id); } @Override public void delete(boolean untrack) { removeTexture(); if(untrack) { - GLStateTracker.TEXTURE_TRACKER.deleteTexture(this); + GLStateTracker.instance().texture_tracker.deleteTexture(this); } } } diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/DynamicTexture.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/DynamicTexture.java index 7bccdab..4aac92a 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/custom/DynamicTexture.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/DynamicTexture.java @@ -10,6 +10,7 @@ import speiger.src.coreengine.assets.base.IAssetProvider; import speiger.src.coreengine.math.BitUtil; import speiger.src.coreengine.rendering.textures.base.BaseTexture; import speiger.src.coreengine.rendering.textures.base.TextureMetadata; +import speiger.src.coreengine.rendering.utils.GLStateTracker; import speiger.src.coreengine.rendering.utils.values.GLDataType; import speiger.src.coreengine.rendering.utils.values.textures.GLTextureFormat; @@ -24,6 +25,7 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture { this.metadata = metadata; this.width = width; this.height = height; + if(width % 16 != 0 || height % 16 != 0) throw new IllegalArgumentException("Texture must be a power of 16"); data = MemoryUtil.nmemAllocChecked(4L * width * height); } @@ -60,28 +62,56 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture { dirtySections.clear(); return; } + GLStateTracker.instance().unpack_row_length.set(width()); Thread thread = Thread.currentThread(); - long tempBuffer = MemoryUtil.nmemAllocChecked(1024); for(IntIterator iter = dirtySections.iterator();iter.hasNext() && !thread.isInterrupted();iter.remove()) { int key = iter.nextInt(); - int x = BitUtil.toFirstShort(key) * 16; - int y = BitUtil.toSecondShort(key) * 16; - for(int i = 0;i<16;i++) { - MemoryUtil.memCopy(data + offset(x, y+i), tempBuffer+i*64, 64);//TODO implement a better solution here, i don't like it - } - GL45.nglTextureSubImage2D(id, 0, x, y, 16, 16, GLTextureFormat.RGBA.glValue(), GLDataType.UNSIGNED_BYTE.glValue(), tempBuffer); + uploadPixels(BitUtil.toFirstShort(key) * 16, BitUtil.toSecondShort(key) * 16); } - MemoryUtil.nmemFree(tempBuffer); + GLStateTracker.instance().unpack_row_length.setDefault(); + GLStateTracker.instance().unpack_skip_pixel.setDefault(); + GLStateTracker.instance().unpack_skip_rows.setDefault(); + } + + protected void uploadPixels(int x, int y) { + GLStateTracker.instance().unpack_skip_pixel.set(x); + GLStateTracker.instance().unpack_skip_rows.set(y); + GL45.nglTextureSubImage2D(id, 0, x, y, 16, 16, GLTextureFormat.RGBA.glValue(), GLDataType.UNSIGNED_BYTE.glValue(), data); } protected long offset(int x, int y) { - return ((y * width()) + x) * 4L; + return ((y * width()) + x) << 2; + } + + protected void ensureValid(int index) { + ensureValid(index % width, index / width); + } + + protected void ensureValid(int x, int y) { + if(x < 0 || y < 0) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"]"); + if(x >= width || y >= height) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"], width=["+width+"], height=["+height+"]"); + } + + @Override + public void fill(int x, int y, int width, int height, int data) { + ensureValid(x, y); + ensureValid(x+width, y+height); + for(int yOff = 0;yOff>4,m=width>>4;i>4,n=height>>4;j> 4, y >> 4)); + ensureValid(x, y); } @Override @@ -116,26 +146,31 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture { @Override public int get(int index) { + ensureValid(index); return Integer.rotateRight(MemoryUtil.memGetInt(index * 4L), 8); } @Override public int getR(int index) { + ensureValid(index); return MemoryUtil.memGetByte(index * 4L); } @Override public int getG(int index) { + ensureValid(index); return MemoryUtil.memGetByte(index * 4L + 1L); } @Override public int getB(int index) { + ensureValid(index); return MemoryUtil.memGetByte(index * 4L + 2L); } @Override public int getA(int index) { + ensureValid(index); return MemoryUtil.memGetByte(index * 4L + 3L); } } \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java index 34e9c05..68f689a 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java @@ -27,6 +27,9 @@ public interface IDynamicTexture extends ITexture { public void setA(int index, int alpha); public default void setA(int x, int y, int alpha) { setA((y * width()) + x, alpha); } + public void fill(int x, int y, int width, int height, int data); + public default void fill(int x, int y, int width, int height, int red, int green, int blue, int alpha) { fill(x, y, width, height, ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF)); } + public int get(int index); public default int get(int x, int y) { return get((y * width()) + x); } diff --git a/src/main/java/speiger/src/coreengine/rendering/texturesOld/base/TextureManager.java b/src/main/java/speiger/src/coreengine/rendering/texturesOld/base/TextureManager.java index 17c72d1..6979de3 100644 --- a/src/main/java/speiger/src/coreengine/rendering/texturesOld/base/TextureManager.java +++ b/src/main/java/speiger/src/coreengine/rendering/texturesOld/base/TextureManager.java @@ -21,7 +21,7 @@ import speiger.src.coreengine.utils.io.GameLog.LogLevel; public class TextureManager implements IReloadableResource { public static final TextureManager INSTANCE = new TextureManager(); - public static final GLState TEXTURE = GLStateTracker.addState(new GLState(GL11.GL_TEXTURE_2D, false)); + public static final GLState TEXTURE = GLStateTracker.instance().addState(new GLState(GL11.GL_TEXTURE_2D, false)); AssetManager manager; ResourceReloader reloader = new ResourceReloader(); int[] textureBanks = new int[GL13.GL_TEXTURE31 - GL13.GL_TEXTURE0]; diff --git a/src/main/java/speiger/src/coreengine/rendering/utils/AllocationTracker.java b/src/main/java/speiger/src/coreengine/rendering/utils/AllocationTracker.java index 6850752..d2d4691 100644 --- a/src/main/java/speiger/src/coreengine/rendering/utils/AllocationTracker.java +++ b/src/main/java/speiger/src/coreengine/rendering/utils/AllocationTracker.java @@ -1,5 +1,8 @@ package speiger.src.coreengine.rendering.utils; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; + import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap; import speiger.src.coreengine.rendering.models.buffers.VertexBuffer; import speiger.src.coreengine.utils.counters.averager.Averager; @@ -13,6 +16,7 @@ public class AllocationTracker { Averager cpuAllocation = new Averager(20); Averager gpuAllocation = new Averager(20); Int2ObjectMap allocatedBuffers = Int2ObjectMap.builder().linkedMap(); + MemoryMXBean offHeap = ManagementFactory.getMemoryMXBean(); public void update() { long newTime = System.currentTimeMillis(); @@ -46,6 +50,10 @@ public class AllocationTracker { lastMemory = memory; } + public long getOffHeapMemory() { + return offHeap.getNonHeapMemoryUsage().getUsed(); + } + private long getMemory() { Runtime runtime = Runtime.getRuntime(); return runtime.totalMemory() - runtime.freeMemory(); diff --git a/src/main/java/speiger/src/coreengine/rendering/utils/GLFunctions.java b/src/main/java/speiger/src/coreengine/rendering/utils/GLFunctions.java new file mode 100644 index 0000000..0678173 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/utils/GLFunctions.java @@ -0,0 +1,21 @@ +package speiger.src.coreengine.rendering.utils; + +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL45; + +import speiger.src.coreengine.rendering.utils.values.IGLValue; + +public class GLFunctions { + + public static int createTexture(IGLValue value) { + return GL45.glCreateTextures(value.glValue()); + } + + public static void deleteTextures(int... id) { + GL11.glDeleteTextures(id); + } + + public static void pixelStoreI(int mode, int value) { + GL11.glPixelStorei(mode, value); + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java b/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java index 9005543..5c51d09 100644 --- a/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java +++ b/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java @@ -19,50 +19,62 @@ import speiger.src.coreengine.rendering.utils.states.GLProvoking; import speiger.src.coreengine.rendering.utils.states.GLState; import speiger.src.coreengine.rendering.utils.states.GLWireFrame; import speiger.src.coreengine.rendering.utils.states.IGLState; +import speiger.src.coreengine.rendering.utils.states.PixelState; import speiger.src.coreengine.rendering.utils.states.TextureState; import speiger.src.coreengine.rendering.utils.values.GLCullType; import speiger.src.coreengine.utils.counters.averager.Counter; public class GLStateTracker { - static final ObjectList ALL_STATES = new ObjectArrayList<>(); + private static final ThreadLocal TRACKER = ThreadLocal.withInitial(GLStateTracker::new); + final ObjectList all_states = new ObjectArrayList<>(); //GPU States - public static final GLState MULTI_SAMPLING = addState(new GLState(GL13.GL_MULTISAMPLE)); - public static final GLWireFrame WIRE_FRAME = addState(new GLWireFrame(GLCullType.BOTH)); - public static final GLState PROVOKING_VERTEX = addState(new GLProvoking()); - public static final GLState DEBTH_TEST = addState(new GLState(GL11.GL_DEPTH_TEST)); - public static final CullState CULL_FACE = addState(new CullState(GLCullType.BACK)); - public static final BlendState BLEND = addState(new BlendState()); - public static final GLState[] CLIP_PLANE0 = addStates(new GLState(GL11.GL_CLIP_PLANE0), new GLState(GL11.GL_CLIP_PLANE1), new GLState(GL11.GL_CLIP_PLANE2), new GLState(GL11.GL_CLIP_PLANE3), new GLState(GL11.GL_CLIP_PLANE4), new GLState(GL11.GL_CLIP_PLANE5)); - public static final FloatState POINT_SIZE = addState(new FloatState(1F, GL11::glPointSize)); - public static final FloatState LINE_SIZE = addState(new FloatState(1F, GL11::glLineWidth)); - public static final ScissorsManager TESTER = new ScissorsManager(100); - public static final Counter[] COUNTERS = Counter.createCounters(4); - public static final TextureState TEXTURES = addState(new TextureState()); - public static final ViewPortStack VIEW_PORT = new ViewPortStack(); + public final GLState multisampling = addState(new GLState(GL13.GL_MULTISAMPLE)); + public final GLWireFrame wireframe = addState(new GLWireFrame(GLCullType.BOTH)); + public final GLState provoking_vertex = addState(new GLProvoking()); + public final GLState debth_test = addState(new GLState(GL11.GL_DEPTH_TEST)); + public final CullState cull_face = addState(new CullState(GLCullType.BACK)); + public final BlendState blend = addState(new BlendState()); + public final GLState[] clip_planes = addStates(new GLState(GL11.GL_CLIP_PLANE0), new GLState(GL11.GL_CLIP_PLANE1), new GLState(GL11.GL_CLIP_PLANE2), new GLState(GL11.GL_CLIP_PLANE3), new GLState(GL11.GL_CLIP_PLANE4), new GLState(GL11.GL_CLIP_PLANE5)); + public final FloatState point_size = addState(new FloatState(1F, GL11::glPointSize)); + public final FloatState line_size = addState(new FloatState(1F, GL11::glLineWidth)); + public final ScissorsManager scissors = new ScissorsManager(100); + public final Counter[] counters = Counter.createCounters(4); + public final TextureState textures = addState(new TextureState()); + public final ViewPortStack viewPort = new ViewPortStack(); + + //Texture States + public final PixelState unpack_alignment = new PixelState(GL11.GL_UNPACK_ALIGNMENT, 4); + public final PixelState unpack_skip_pixel = new PixelState(GL11.GL_UNPACK_SKIP_PIXELS, 0); + public final PixelState unpack_skip_rows = new PixelState(GL11.GL_UNPACK_SKIP_ROWS, 0); + public final PixelState unpack_row_length = new PixelState(GL11.GL_UNPACK_ROW_LENGTH, 0); //Trackers - public static final ShaderTracker SHADERS = new ShaderTracker(); - public static final TextureTracker TEXTURE_TRACKER = new TextureTracker(); - public static final GlobalUniforms UNIFORMS = new GlobalUniforms(); + public final ShaderTracker shaders = new ShaderTracker(); + public final TextureTracker texture_tracker = new TextureTracker(); + public final GlobalUniforms uniforms = new GlobalUniforms(); - public static T addState(T state) { - ALL_STATES.add(state); + public static GLStateTracker instance() { + return TRACKER.get(); + } + + public T addState(T state) { + all_states.add(state); return state; } - @SafeVarargs - public static T[] addStates(T... states) { - ALL_STATES.addAll(states); + @SuppressWarnings("unchecked") + public T[] addStates(T... states) { + all_states.addAll(states); return states; } - public static void reapplyState() { - ALL_STATES.forEach(IGLState::reapply); + public void reapplyState() { + all_states.forEach(IGLState::reapply); } - public static void onFrameEnded() { - for(int i = 0,m = COUNTERS.length;i < m;i++) { - COUNTERS[i].onFinished(); + public void onFrameEnded() { + for(int i = 0,m = counters.length;i < m;i++) { + counters[i].onFinished(); } } @@ -106,29 +118,29 @@ public class GLStateTracker { } static void addVerties(int mode, int count) { - COUNTERS[0].add(count); - switch(mode) { - case GL11.GL_POINTS: - COUNTERS[1].add(count); - break; - case GL11.GL_LINES: - COUNTERS[2].add(count / 2); - break; - case GL11.GL_LINE_LOOP: - COUNTERS[2].add((count + 2) / 2); - break; - case GL11.GL_LINE_STRIP: - COUNTERS[2].add(count - 1); - break; - case GL11.GL_TRIANGLES: - COUNTERS[3].add(count / 3); - break; - case GL11.GL_TRIANGLE_STRIP: - COUNTERS[3].add(count - 2); - break; - case GL11.GL_TRIANGLE_FAN: - COUNTERS[3].add(count - 2); - break; - } +// counters[0].add(count); +// switch(mode) { +// case GL11.GL_POINTS: +// counters[1].add(count); +// break; +// case GL11.GL_LINES: +// counters[2].add(count / 2); +// break; +// case GL11.GL_LINE_LOOP: +// counters[2].add((count + 2) / 2); +// break; +// case GL11.GL_LINE_STRIP: +// counters[2].add(count - 1); +// break; +// case GL11.GL_TRIANGLES: +// counters[3].add(count / 3); +// break; +// case GL11.GL_TRIANGLE_STRIP: +// counters[3].add(count - 2); +// break; +// case GL11.GL_TRIANGLE_FAN: +// counters[3].add(count - 2); +// break; +// } } } diff --git a/src/main/java/speiger/src/coreengine/rendering/utils/states/PixelState.java b/src/main/java/speiger/src/coreengine/rendering/utils/states/PixelState.java new file mode 100644 index 0000000..646be93 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/utils/states/PixelState.java @@ -0,0 +1,41 @@ +package speiger.src.coreengine.rendering.utils.states; + +import speiger.src.coreengine.rendering.utils.GLFunctions; + +public class PixelState implements IGLState { + final int id; + final int defaultValue; + int value; + + public PixelState(int id, int defaultValue) { + this.id = id; + this.defaultValue = defaultValue; + this.value = defaultValue; + } + + public PixelState set(int value) { + if(this.value != value) { + this.value = value; + setValue(value); + } + return this; + } + + protected void setValue(int value) { + GLFunctions.pixelStoreI(id, value); + } + + public int get() { + return value; + } + + @Override + public void setDefault() { + set(defaultValue); + } + + @Override + public void reapply() { + setValue(value); + } +} diff --git a/src/main/java/speiger/src/coreengine/utils/collections/CollectionUtils.java b/src/main/java/speiger/src/coreengine/utils/collections/CollectionUtils.java index d0746a5..24a2966 100644 --- a/src/main/java/speiger/src/coreengine/utils/collections/CollectionUtils.java +++ b/src/main/java/speiger/src/coreengine/utils/collections/CollectionUtils.java @@ -20,6 +20,7 @@ public class CollectionUtils { return result; } + @SuppressWarnings("unchecked") public static Set[] createSets(int size, boolean linked) { Set[] sets = new Set[size]; for(int i = 0;i < size;i++) { @@ -28,6 +29,7 @@ public class CollectionUtils { return sets; } + @SuppressWarnings("unchecked") public static ObjectList[] createList(int size) { ObjectList[] list = new ObjectList[size]; for(int i = 0;i < size;i++) { @@ -36,6 +38,7 @@ public class CollectionUtils { return list; } + @SuppressWarnings("unchecked") public static Int2ObjectMap[] createInt2ObjectMap(int size, boolean linked) { Int2ObjectMap[] maps = new Int2ObjectMap[size]; for(int i = 0;i < size;i++) { diff --git a/src/main/java/speiger/src/coreengine/utils/counters/averager/Averager.java b/src/main/java/speiger/src/coreengine/utils/counters/averager/Averager.java index c18c7e1..7166d26 100644 --- a/src/main/java/speiger/src/coreengine/utils/counters/averager/Averager.java +++ b/src/main/java/speiger/src/coreengine/utils/counters/averager/Averager.java @@ -19,7 +19,7 @@ public class Averager { public void addEntry(long time) { totalValue += time; - if(values.size() >= totalCount) { totalValue -= values.dequeue(); } + if(values.size() >= totalCount) totalValue -= values.dequeue(); values.enqueue(time); } diff --git a/src/main/java/speiger/src/coreengine/utils/eventbus/EventBus.java b/src/main/java/speiger/src/coreengine/utils/eventbus/EventBus.java index db709d2..441cd4b 100644 --- a/src/main/java/speiger/src/coreengine/utils/eventbus/EventBus.java +++ b/src/main/java/speiger/src/coreengine/utils/eventbus/EventBus.java @@ -3,22 +3,20 @@ package speiger.src.coreengine.utils.eventbus; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles.Lookup; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.List; import java.util.function.Consumer; +import java.util.function.Function; import speiger.src.collections.objects.lists.ObjectArrayList; -import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap; +import speiger.src.collections.objects.maps.impl.concurrent.Object2ObjectConcurrentOpenHashMap; import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap; import speiger.src.collections.objects.utils.ObjectLists; -import speiger.src.collections.objects.utils.maps.Object2ObjectMaps; public class EventBus { private static final Lookup LOOKUP = MethodHandles.lookup(); - Object2ObjectMap, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>()); - Object2ObjectMap> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>()); + Object2ObjectMap, Listeners> listeners = new Object2ObjectConcurrentOpenHashMap<>(); + Object2ObjectMap> instances = new Object2ObjectConcurrentOpenHashMap<>(); public void register(Class event, Consumer listener) { register(event, EventPriority.MEDIUM, listener); @@ -39,7 +37,7 @@ public class EventBus final List list = new ObjectArrayList<>(); try { register(obj.getClass().getDeclaredAnnotation(SubscribeEvent.class), obj, list); - findMethods(obj.getClass(), superClasses, t -> { + find(obj.getClass(), superClasses, Class::getDeclaredMethods, t -> { try { t.setAccessible(true); SubscribeEvent data = t.getAnnotation(SubscribeEvent.class); @@ -50,7 +48,7 @@ public class EventBus } catch(Exception e) { e.printStackTrace(); } }); - findFields(obj.getClass(), superClasses, t -> { + find(obj.getClass(), superClasses, Class::getDeclaredFields, t -> { try { t.setAccessible(true); register(t.getAnnotation(SubscribeEvent.class), t.get(obj), list); @@ -69,22 +67,15 @@ public class EventBus listeners.add(new EventListener(data.value(), cast(obj))); } - private void findFields(Class clz, boolean superClasses, Consumer fields) { + private void find(Class clz, boolean superClasses, Function, T[]> mapper, Consumer result) { do { - for(Field field : clz.getDeclaredFields()) fields.accept(field); + for(T value : mapper.apply(clz)) result.accept(value); clz = clz.getSuperclass(); } while(clz != Object.class && superClasses); + } - - private void findMethods(Class clz, boolean superClasses, Consumer methods) { - do { - for(Method field : clz.getDeclaredMethods()) methods.accept(field); - clz = clz.getSuperclass(); - } - while(clz != Object.class && superClasses); - } - + public void unregister(Object obj) { for(EventListener entry : instances.remOrDefault(obj, ObjectLists.empty())) { getListeners(entry.event()).removeListeners(entry.listener()); diff --git a/src/main/java/speiger/src/coreengine/utils/io/dataTag/parsing/DataTagIOBuilder.java b/src/main/java/speiger/src/coreengine/utils/io/dataTag/parsing/DataTagIOBuilder.java index 996b2b6..cd2f5d5 100644 --- a/src/main/java/speiger/src/coreengine/utils/io/dataTag/parsing/DataTagIOBuilder.java +++ b/src/main/java/speiger/src/coreengine/utils/io/dataTag/parsing/DataTagIOBuilder.java @@ -23,6 +23,7 @@ public class DataTagIOBuilder return builder; } + @SuppressWarnings("unchecked") public DataTagIOBuilder add(Class type, Object obj) { IDataSerializer serializer = obj instanceof IDataSerializer ? (IDataSerializer)obj : null;