Skip to content

Commit

Permalink
cache attribute formats
Browse files Browse the repository at this point in the history
  • Loading branch information
DaMatrix committed Feb 10, 2022
1 parent 10de630 commit 47b6f98
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
10 changes: 10 additions & 0 deletions gl/opengl/src/main/java/net/daporkchop/fp2/gl/opengl/OpenGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

package net.daporkchop.fp2.gl.opengl;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import lombok.Getter;
import lombok.NonNull;
Expand All @@ -28,6 +31,7 @@
import net.daporkchop.fp2.common.util.alloc.Allocator;
import net.daporkchop.fp2.common.util.alloc.DirectMemoryAllocator;
import net.daporkchop.fp2.gl.GL;
import net.daporkchop.fp2.gl.attribute.AttributeFormat;
import net.daporkchop.fp2.gl.attribute.AttributeFormatBuilder;
import net.daporkchop.fp2.gl.attribute.BufferUsage;
import net.daporkchop.fp2.gl.attribute.texture.TextureFormat2D;
Expand All @@ -49,6 +53,8 @@
import net.daporkchop.fp2.gl.draw.shader.FragmentShader;
import net.daporkchop.fp2.gl.draw.shader.VertexShader;
import net.daporkchop.fp2.gl.opengl.attribute.AttributeFormatBuilderImpl;
import net.daporkchop.fp2.gl.opengl.attribute.AttributeFormatType;
import net.daporkchop.fp2.gl.opengl.attribute.common.AttributeFormatImpl;
import net.daporkchop.fp2.gl.opengl.attribute.struct.StructFormatGenerator;
import net.daporkchop.fp2.gl.opengl.attribute.texture.TextureFormat2DImpl;
import net.daporkchop.fp2.gl.opengl.attribute.texture.TextureFormatBuilderImpl;
Expand Down Expand Up @@ -116,6 +122,10 @@ public class OpenGL implements GL {

protected final StructFormatGenerator structFormatGenerator = new StructFormatGenerator();

protected final LoadingCache<AttributeFormatBuilderImpl<?>, AttributeFormat<?>> attributeFormatCache = CacheBuilder.newBuilder()
.weakValues()
.build(CacheLoader.from(AttributeFormatType::createBestFormat));

protected final int vertexAttributeAlignment;

protected final boolean preserveInputGlState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

package net.daporkchop.fp2.gl.opengl.attribute;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import net.daporkchop.fp2.gl.attribute.AttributeFormat;
import net.daporkchop.fp2.gl.attribute.AttributeFormatBuilder;
import net.daporkchop.fp2.gl.attribute.AttributeUsage;
Expand All @@ -36,14 +39,15 @@
import java.util.Set;

import static net.daporkchop.lib.common.util.PValidation.*;
import static net.daporkchop.lib.common.util.PorkUtil.*;

/**
* @author DaPorkchop_
*/
@RequiredArgsConstructor
@Getter
@Data
public class AttributeFormatBuilderImpl<S> implements AttributeFormatBuilder<S> {
@NonNull
@EqualsAndHashCode.Exclude
protected final OpenGL gl;
@NonNull
protected final Class<S> clazz;
Expand Down Expand Up @@ -71,7 +75,7 @@ public AttributeFormatBuilder<S> useFor(@NonNull AttributeUsage... usages) {

@Override
public AttributeFormat<S> build() {
return AttributeFormatType.createBestFormat(this);
return uncheckedCast(this.gl.attributeFormatCache().getUnchecked(this));
}

public StructInfo<S> structInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.daporkchop.fp2.gl.opengl.attribute.format.ArrayPackedAttributeFormat;
import net.daporkchop.fp2.gl.opengl.attribute.format.ArrayPackedInstancedAttributeFormat;
import net.daporkchop.fp2.gl.opengl.attribute.format.ArrayUnpackedAttributeFormat;
import net.daporkchop.fp2.gl.opengl.attribute.format.ArrayUnpackedInstancedAttributeFormat;
import net.daporkchop.fp2.gl.opengl.attribute.format.Std140BlockAttributeFormat;

import java.util.Optional;
Expand All @@ -51,6 +52,14 @@ public enum AttributeFormatType {
: Optional.empty();
}
},
ARRAY_UNPACKED_INSTANCED {
@Override
public <S> Optional<AttributeFormatImpl<?, S, ?>> createFormat(@NonNull AttributeFormatBuilderImpl<S> builder) {
return ArrayUnpackedInstancedAttributeFormat.supports(builder)
? Optional.of(new ArrayUnpackedInstancedAttributeFormat<>(builder))
: Optional.empty();
}
},
ARRAY_UNPACKED {
@Override
public <S> Optional<AttributeFormatImpl<?, S, ?>> createFormat(@NonNull AttributeFormatBuilderImpl<S> builder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2020-2022 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* Any persons and/or organizations using this software must include the above copyright notice and this permission notice,
* provide sufficient credit to the original authors of the project (IE: DaPorkchop_), as well as provide a link to the original project.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

package net.daporkchop.fp2.gl.opengl.attribute.format;

import com.google.common.collect.ImmutableSet;
import lombok.NonNull;
import net.daporkchop.fp2.gl.attribute.AttributeUsage;
import net.daporkchop.fp2.gl.opengl.attribute.AttributeFormatBuilderImpl;
import net.daporkchop.fp2.gl.opengl.attribute.binding.BindingLocation;
import net.daporkchop.fp2.gl.opengl.attribute.binding.BindingLocationAssigner;
import net.daporkchop.fp2.gl.opengl.attribute.common.interleaved.InterleavedAttributeFormatImpl;
import net.daporkchop.fp2.gl.opengl.attribute.common.interleaved.draw.global.InterleavedDrawGlobalAttributeBindingLocation;
import net.daporkchop.fp2.gl.opengl.attribute.common.interleaved.draw.local.InterleavedDrawLocalAttributeBindingLocation;
import net.daporkchop.fp2.gl.opengl.attribute.common.interleaved.transform.input.InterleavedTransformInputAttributeBindingLocation;
import net.daporkchop.fp2.gl.opengl.attribute.common.interleaved.transform.output.InterleavedTransformOutputAttributeBindingLocation;
import net.daporkchop.fp2.gl.opengl.attribute.struct.StructLayouts;
import net.daporkchop.fp2.gl.opengl.layout.LayoutEntry;

import java.util.EnumSet;
import java.util.Set;

/**
* @author DaPorkchop_
*/
public final class ArrayUnpackedInstancedAttributeFormat<S> extends InterleavedAttributeFormatImpl<ArrayUnpackedInstancedAttributeFormat<S>, S> {
private static final Set<AttributeUsage> VALID_USAGES = ImmutableSet.copyOf(EnumSet.of(
AttributeUsage.DRAW_GLOBAL,
AttributeUsage.DRAW_LOCAL,
AttributeUsage.TRANSFORM_INPUT,
AttributeUsage.TRANSFORM_OUTPUT
));

public static boolean supports(@NonNull AttributeFormatBuilderImpl<?> builder) {
return VALID_USAGES.containsAll(builder.usages());
}

public ArrayUnpackedInstancedAttributeFormat(@NonNull AttributeFormatBuilderImpl<S> builder) {
super(builder.gl(), StructLayouts.vertexAttributesInterleaved(builder.gl(), builder.structInfo(), true));
}

@Override
public Set<AttributeUsage> validUsages() {
return VALID_USAGES;
}

@Override
public BindingLocation<?> bindingLocation(@NonNull LayoutEntry<ArrayUnpackedInstancedAttributeFormat<S>> layout, @NonNull BindingLocationAssigner assigner) {
switch (layout.usage()) {
case DRAW_GLOBAL:
return new InterleavedDrawGlobalAttributeBindingLocation<>(layout, assigner);
case DRAW_LOCAL:
return new InterleavedDrawLocalAttributeBindingLocation<>(layout, assigner);
case TRANSFORM_INPUT:
return new InterleavedTransformInputAttributeBindingLocation<>(layout, assigner);
case TRANSFORM_OUTPUT:
return new InterleavedTransformOutputAttributeBindingLocation<>(layout, assigner);
default:
throw new IllegalArgumentException("unsupported usage: " + layout.usage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -74,6 +75,7 @@
public class StructPropertyFactory {
public static StructProperty struct(@NonNull Options options, @NonNull Class<?> struct, @NonNull Map<String, String> nameOverrides) {
Map<String, Field> fieldsByName = Stream.of(struct.getFields())
.filter(field -> (field.getModifiers() & Modifier.STATIC) == 0) //skip static fields
.collect(Collectors.toMap(Field::getName, Function.identity(), (a, b) -> {
throw new IllegalArgumentException(a + " " + b);
},
Expand Down Expand Up @@ -153,7 +155,7 @@ public static StructProperty attributeFromField(@NonNull Options options, @NonNu
if (componentType.getType() instanceof Class && ((Class<?>) componentType.getType()).isPrimitive()) {
property = new SimplePrimitiveArrayInputProperty(options, field, arrayType.length());
} else {
throw new UnsupportedOperationException("don't know how to process type: " + annotatedArrayType);
throw new UnsupportedOperationException("don't know how to process type: " + annotatedArrayType.getType());
}

return arrayTransform(options, property, arrayType.transform());
Expand All @@ -168,7 +170,7 @@ public static StructProperty attributeFromField(@NonNull Options options, @NonNu

public static StructProperty processAnnotated(@NonNull Options options, @NonNull StructProperty property, @NonNull AnnotatedType annotatedType) {
if (annotatedType instanceof AnnotatedArrayType) {
throw new UnsupportedOperationException("don't know how to process type: " + annotatedType);
throw new UnsupportedOperationException("don't know how to process type: " + annotatedType.getType());
} else {
return annotatedType.isAnnotationPresent(ScalarType.class)
? scalarType(options, (StructProperty.Components) property, annotatedType.getAnnotation(ScalarType.class))
Expand Down

0 comments on commit 47b6f98

Please sign in to comment.