More Reworks

This commit is contained in:
Speiger 2024-04-20 21:34:56 +02:00
parent 84e84d6b26
commit 7cedfaf9bd
28 changed files with 271 additions and 147 deletions

View File

@ -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));

View File

@ -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

View File

@ -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();
}

View File

@ -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;
}

View File

@ -121,12 +121,12 @@ public class Window
for(int i = 0,m=callbacks.size();i<m;i++) {
callbacks.get(i).reload(windowId);
}
GLStateTracker.reapplyState();
GLStateTracker.instance().reapplyState();
}
protected void updateViewPort() {
flags.clearFlag(FLAG_WINDOW_CHANGE);
GLStateTracker.VIEW_PORT.setDefault(0, 0, width, height);
GLStateTracker.instance().viewPort.setDefault(0, 0, width, height);
createProjectionMatrix();
ui_frame.updateScale();
for(int i = 0,m=listeners.size();i<m;i++) {

View File

@ -24,7 +24,7 @@ public class IndirectArrayBuffer extends VertexBuffer implements Iterable<DrawAr
for(int i = 0;i<size;i++) {
commands.add(new DrawArraysIndirectCommand());
}
bind().allocate(size * 16).unbind();
allocate(size * 16);
}
public int commands() {

View File

@ -8,7 +8,7 @@ import java.nio.ShortBuffer;
import java.util.List;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL32;
import org.lwjgl.opengl.GL45;
import org.lwjgl.system.MemoryUtil;
import speiger.src.collections.ints.misc.pairs.IntObjectPair;
@ -24,7 +24,7 @@ public class VertexBuffer {
protected int size;
public VertexBuffer(IGLBuffer type) {
this(type, GL15.glGenBuffers());
this(type, GL45.glCreateBuffers());
AllocationTracker.INSTANCE.track(this);
}
@ -66,26 +66,26 @@ public class VertexBuffer {
public VertexBuffer allocate(int totalBytes) {
this.size = totalBytes;
GL15.glBufferData(type.glValue(), totalBytes, state.glValue());
GL45.glNamedBufferData(id, totalBytes, state.glValue());
AllocationTracker.INSTANCE.addGPUBytes(totalBytes);
return this;
}
public VertexBuffer set(long pointer, int totalBytes) {
this.size = totalBytes;
GL15.nglBufferData(type.glValue(), totalBytes, pointer, state.glValue());
GL45.nglNamedBufferData(id, totalBytes, pointer, state.glValue());
AllocationTracker.INSTANCE.addGPUBytes(totalBytes);
return this;
}
public VertexBuffer fill(long pointer, int totalBytes, int offset) {
GL15.nglBufferSubData(type.glValue(), offset, totalBytes, pointer);
GL45.nglNamedBufferSubData(id, offset, totalBytes, pointer);
AllocationTracker.INSTANCE.addGPUBytes(totalBytes);
return this;
}
public VertexBuffer read(long pointer, int totalBytes, int offset) {
GL15.nglGetBufferSubData(type.glValue(), offset, totalBytes, pointer);
GL45.nglGetNamedBufferSubData(id, offset, totalBytes, pointer);
AllocationTracker.INSTANCE.addGPUBytes(totalBytes);
return this;
}
@ -139,29 +139,29 @@ public class VertexBuffer {
}
public VertexBuffer fill(int offset, byte[] data) {
GL15.glMapBuffer(type.glValue(), GL15.GL_WRITE_ONLY).position(offset).put(data);
GL15.glUnmapBuffer(type.glValue());
GL45.glMapNamedBuffer(id, GL15.GL_WRITE_ONLY).position(offset).put(data);
GL45.glUnmapNamedBuffer(id);
AllocationTracker.INSTANCE.addGPUBytes(data.length);
return this;
}
public VertexBuffer fill(int offset, short[] data) {
GL15.glMapBuffer(type.glValue(), GL15.GL_WRITE_ONLY).position(offset).asShortBuffer().put(data);
GL15.glUnmapBuffer(type.glValue());
GL45.glMapNamedBuffer(id, GL15.GL_WRITE_ONLY).position(offset).asShortBuffer().put(data);
GL45.glUnmapNamedBuffer(id);
AllocationTracker.INSTANCE.addGPUBytes(data.length * 2L);
return this;
}
public VertexBuffer fill(int offset, int[] data) {
GL15.glMapBuffer(type.glValue(), GL15.GL_WRITE_ONLY).position(offset).asIntBuffer().put(data);
GL15.glUnmapBuffer(type.glValue());
GL45.glMapNamedBuffer(id, GL15.GL_WRITE_ONLY).position(offset).asIntBuffer().put(data);
GL45.glUnmapNamedBuffer(id);
AllocationTracker.INSTANCE.addGPUBytes(data.length * 4L);
return this;
}
public VertexBuffer fill(int offset, float[] data) {
GL15.glMapBuffer(type.glValue(), GL15.GL_WRITE_ONLY).position(offset).asFloatBuffer().put(data);
GL15.glUnmapBuffer(type.glValue());
GL45.glMapNamedBuffer(id, GL15.GL_WRITE_ONLY).position(offset).asFloatBuffer().put(data);
GL45.glUnmapNamedBuffer(id);
AllocationTracker.INSTANCE.addGPUBytes(data.length * 4L);
return this;
}
@ -173,7 +173,7 @@ public class VertexBuffer {
public VertexBuffer fill(int offset, List<IntObjectPair<byte[]>> 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<byte[]> 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;
}

View File

@ -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;
}

View File

@ -20,7 +20,7 @@ public class Shader<T extends ShaderProgram> implements Supplier<T> {
public static <T extends ShaderProgram> Shader<T> createAndRegister(Function<IAssetProvider, T> provider) {
Shader<T> shader = new Shader<>(provider);
GLStateTracker.SHADERS.register(shader);
GLStateTracker.instance().shaders.register(shader);
return shader;
}
@ -42,7 +42,7 @@ public class Shader<T extends ShaderProgram> implements Supplier<T> {
public void removeProgram() {
remove();
GLStateTracker.SHADERS.remove(this);
GLStateTracker.instance().shaders.remove(this);
}
@Override

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -29,11 +29,11 @@ public class UniformManager {
}
public <T extends IUniform> T addGlobalUniform(String name) {
return addUniform(GLStateTracker.UNIFORMS.get(name));
return addUniform(GLStateTracker.instance().uniforms.get(name));
}
public <T extends IUniform> 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) {

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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<height;yOff++) {
MemoryUtil.memSet(offset(x, y+yOff), data, width * 4L);
}
if(id() == 0) return;
for(int i = x>>4,m=width>>4;i<m;i++) {
for(int j = y>>4,n=height>>4;j<n;j++) {
dirtySections.add(BitUtil.toInt(i, j));
}
}
}
@Override
public void dirty(int x, int y) {
if(id() == 0) return;
dirtySections.add(BitUtil.toInt(x >> 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);
}
}

View File

@ -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); }

View File

@ -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];

View File

@ -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<VertexBuffer> 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();

View File

@ -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);
}
}

View File

@ -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<IGLState> ALL_STATES = new ObjectArrayList<>();
private static final ThreadLocal<GLStateTracker> TRACKER = ThreadLocal.withInitial(GLStateTracker::new);
final ObjectList<IGLState> 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 extends IGLState> T addState(T state) {
ALL_STATES.add(state);
public static GLStateTracker instance() {
return TRACKER.get();
}
public <T extends IGLState> T addState(T state) {
all_states.add(state);
return state;
}
@SafeVarargs
public static <T extends IGLState> T[] addStates(T... states) {
ALL_STATES.addAll(states);
@SuppressWarnings("unchecked")
public <T extends IGLState> 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;
// }
}
}

View File

@ -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);
}
}

View File

@ -20,6 +20,7 @@ public class CollectionUtils {
return result;
}
@SuppressWarnings("unchecked")
public static <T> Set<T>[] createSets(int size, boolean linked) {
Set<T>[] sets = new Set[size];
for(int i = 0;i < size;i++) {
@ -28,6 +29,7 @@ public class CollectionUtils {
return sets;
}
@SuppressWarnings("unchecked")
public static <T> ObjectList<T>[] createList(int size) {
ObjectList<T>[] list = new ObjectList[size];
for(int i = 0;i < size;i++) {
@ -36,6 +38,7 @@ public class CollectionUtils {
return list;
}
@SuppressWarnings("unchecked")
public static <T> Int2ObjectMap<T>[] createInt2ObjectMap(int size, boolean linked) {
Int2ObjectMap<T>[] maps = new Int2ObjectMap[size];
for(int i = 0;i < size;i++) {

View File

@ -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);
}

View File

@ -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<Class<? extends Event>, Listeners> listeners = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
Object2ObjectMap<Object, List<EventListener>> instances = Object2ObjectMaps.synchronize(new Object2ObjectOpenHashMap<>());
Object2ObjectMap<Class<? extends Event>, Listeners> listeners = new Object2ObjectConcurrentOpenHashMap<>();
Object2ObjectMap<Object, List<EventListener>> instances = new Object2ObjectConcurrentOpenHashMap<>();
public <T extends Event> void register(Class<T> event, Consumer<T> listener) {
register(event, EventPriority.MEDIUM, listener);
@ -39,7 +37,7 @@ public class EventBus
final List<EventListener> 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<Field> fields) {
private <T> void find(Class<?> clz, boolean superClasses, Function<Class<?>, T[]> mapper, Consumer<T> 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<Method> 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());

View File

@ -23,6 +23,7 @@ public class DataTagIOBuilder
return builder;
}
@SuppressWarnings("unchecked")
public <T> DataTagIOBuilder add(Class<T> type, Object obj)
{
IDataSerializer<T> serializer = obj instanceof IDataSerializer ? (IDataSerializer<T>)obj : null;