Skip to content

Commit

Permalink
refactor: Clean up buffer APIs
Browse files Browse the repository at this point in the history
This prevents a lot of invalid usages and makes it easier for sodium-gfx
to optimize for things.
  • Loading branch information
jellysquid3 committed Apr 12, 2022
1 parent e565ddc commit 6e42d38
Show file tree
Hide file tree
Showing 31 changed files with 334 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
import net.caffeinemc.gfx.api.array.attribute.VertexAttributeFormat;
import net.caffeinemc.gfx.api.buffer.BufferMapFlags;
import net.caffeinemc.gfx.api.buffer.BufferStorageFlags;
import net.caffeinemc.gfx.api.shader.ShaderType;
import net.caffeinemc.gfx.api.pipeline.state.BlendFunc;
import net.caffeinemc.gfx.api.pipeline.state.DepthFunc;
import net.caffeinemc.gfx.api.shader.ShaderType;
import net.caffeinemc.gfx.api.types.ElementFormat;
import net.caffeinemc.gfx.api.types.PrimitiveType;
import org.lwjgl.opengl.GL45C;
Expand Down Expand Up @@ -64,25 +62,6 @@ public class GlEnum {
map.put(ElementFormat.UNSIGNED_INT, GL45C.GL_UNSIGNED_INT);
});

private static final int[] BUFFER_MAP_FLAGS = build(BufferMapFlags.class, (map) -> {
map.put(BufferMapFlags.READ, GL45C.GL_MAP_READ_BIT);
map.put(BufferMapFlags.WRITE, GL45C.GL_MAP_WRITE_BIT);
map.put(BufferMapFlags.PERSISTENT, GL45C.GL_MAP_PERSISTENT_BIT);
map.put(BufferMapFlags.INVALIDATE_BUFFER, GL45C.GL_MAP_INVALIDATE_BUFFER_BIT);
map.put(BufferMapFlags.INVALIDATE_RANGE, GL45C.GL_MAP_INVALIDATE_RANGE_BIT);
map.put(BufferMapFlags.EXPLICIT_FLUSH, GL45C.GL_MAP_FLUSH_EXPLICIT_BIT);
map.put(BufferMapFlags.UNSYNCHRONIZED, GL45C.GL_MAP_UNSYNCHRONIZED_BIT);
map.put(BufferMapFlags.COHERENT, GL45C.GL_MAP_COHERENT_BIT);
});

private static final int[] BUFFER_STORAGE_FLAGS = build(BufferStorageFlags.class, (map) -> {
map.put(BufferStorageFlags.PERSISTENT, GL45C.GL_MAP_PERSISTENT_BIT);
map.put(BufferStorageFlags.READABLE, GL45C.GL_MAP_READ_BIT);
map.put(BufferStorageFlags.WRITABLE, GL45C.GL_MAP_WRITE_BIT);
map.put(BufferStorageFlags.CLIENT_STORAGE, GL45C.GL_CLIENT_STORAGE_BIT);
map.put(BufferStorageFlags.COHERENT, GL45C.GL_MAP_COHERENT_BIT);
});

private static final int[] DEPTH_FUNC = build(DepthFunc.class, (map) -> {
map.put(DepthFunc.NEVER, GL45C.GL_NEVER);
map.put(DepthFunc.LESS, GL45C.GL_LESS);
Expand Down Expand Up @@ -141,26 +120,6 @@ public static int from(ShaderType value) {
return SHADER_TYPES[value.ordinal()];
}

public static int mapFlags(Iterable<BufferMapFlags> values) {
int result = 0;

for (BufferMapFlags value : values) {
result |= BUFFER_MAP_FLAGS[value.ordinal()];
}

return result;
}

public static int storageFlags(Iterable<BufferStorageFlags> values) {
int result = 0;

for (BufferStorageFlags value : values) {
result |= BUFFER_STORAGE_FLAGS[value.ordinal()];
}

return result;
}

private static <T extends Enum<T>> int[] build(Class<T> type, Consumer<Reference2IntMap<T>> consumer) {
Enum<T>[] universe = type.getEnumConstants();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@ protected GlObject() {
}

protected final void setHandle(int handle) {
if (handle == INVALID_HANDLE) {
throw new IllegalArgumentException("Handle must be valid");
}

this.handle = handle;
}

public final int handle() {
this.checkHandle();

return this.handle;
}

protected final void checkHandle() {
if (!this.isHandleValid()) {
throw new IllegalStateException("%s is not valid"
if (this.handle == INVALID_HANDLE) {
throw new IllegalStateException("%s handle is not valid"
.formatted(this.getClass().getSimpleName()));
}
}

private boolean isHandleValid() {
return this.handle != INVALID_HANDLE;
return this.handle;
}

public final void invalidateHandle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public T[] getBufferTargets() {
return this.desc.targets();
}

public static int getHandle(VertexArray<?> array) {
public static int handle(VertexArray<?> array) {
return ((GlVertexArray<?>) array).handle();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package net.caffeinemc.gfx.opengl.buffer;

import net.caffeinemc.gfx.opengl.GlObject;
import net.caffeinemc.gfx.api.buffer.Buffer;
import net.caffeinemc.gfx.opengl.GlObject;

public class GlBuffer extends GlObject implements Buffer {
public class GlAbstractBuffer extends GlObject implements Buffer {
private final long capacity;

public GlBuffer(long capacity, int handle) {
public GlAbstractBuffer(int handle, long capacity) {
this.setHandle(handle);

this.capacity = capacity;
}

public static int handle(Buffer buffer) {
return ((GlBuffer) buffer).handle();
return ((GlAbstractBuffer) buffer).handle();
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.caffeinemc.gfx.opengl.buffer;

import net.caffeinemc.gfx.api.buffer.DynamicBuffer;
import net.caffeinemc.gfx.api.buffer.DynamicBufferFlags;

import java.util.Collections;
import java.util.Set;

public class GlDynamicBuffer extends GlAbstractBuffer implements DynamicBuffer {
private final Set<DynamicBufferFlags> flags;

public GlDynamicBuffer(int handle, long capacity, Set<DynamicBufferFlags> flags) {
super(handle, capacity);
this.flags = Collections.unmodifiableSet(flags);
}

@Override
public Set<DynamicBufferFlags> flags() {
return this.flags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.caffeinemc.gfx.opengl.buffer;

import net.caffeinemc.gfx.api.buffer.ImmutableBuffer;
import net.caffeinemc.gfx.api.buffer.ImmutableBufferFlags;

import java.util.Collections;
import java.util.Set;

public class GlImmutableBuffer extends GlAbstractBuffer implements ImmutableBuffer {
private final Set<ImmutableBufferFlags> flags;

public GlImmutableBuffer(int handle, long capacity, Set<ImmutableBufferFlags> flags) {
super(handle, capacity);
this.flags = Collections.unmodifiableSet(flags);
}

@Override
public Set<ImmutableBufferFlags> flags() {
return this.flags;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
package net.caffeinemc.gfx.opengl.buffer;

import net.caffeinemc.gfx.api.buffer.BufferMapFlags;
import net.caffeinemc.gfx.api.buffer.MappedBufferFlags;
import net.caffeinemc.gfx.api.buffer.MappedBuffer;
import net.caffeinemc.gfx.api.device.RenderConfiguration;
import org.apache.commons.lang3.Validate;
import org.lwjgl.opengl.GL45C;

import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Set;

public class GlMappedBuffer extends GlBuffer implements MappedBuffer {
private final ByteBuffer data;
private final Set<BufferMapFlags> flags;
public class GlMappedBuffer extends GlAbstractBuffer implements MappedBuffer {
private final ByteBuffer view;
private final Set<MappedBufferFlags> flags;

public GlMappedBuffer(long capacity, int handle, ByteBuffer data, Set<BufferMapFlags> flags) {
super(capacity, handle);
public GlMappedBuffer(int handle, ByteBuffer view, Set<MappedBufferFlags> flags) {
super(handle, view.capacity());

this.data = !flags.contains(BufferMapFlags.WRITE) ? data.asReadOnlyBuffer() : data;
this.flags = flags;
this.view = view;
this.flags = Collections.unmodifiableSet(flags);
}

@Override
public void flush(long pos, long length) {
if (this.flags.contains(BufferMapFlags.EXPLICIT_FLUSH)) {
GL45C.glFlushMappedNamedBufferRange(this.handle(), pos, length);
public void flush(long offset, long length) {
if (RenderConfiguration.API_CHECKS) {
Validate.isTrue(this.flags.contains(MappedBufferFlags.EXPLICIT_FLUSH), "Buffer is not mapped for explicit flushing");

Validate.isTrue(offset >= 0, "The offset must be greater than or equal to zero");
Validate.isTrue(offset + length <= this.capacity(), "Range is outside of buffer bounds");
}

GL45C.glFlushMappedNamedBufferRange(this.handle(), offset, length);
}

@Override
public ByteBuffer view() {
return this.data;
return this.view;
}

@Override
public Set<MappedBufferFlags> flags() {
return this.flags;
}
}
Loading

0 comments on commit 6e42d38

Please sign in to comment.