forked from FiniteReality/embeddium
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
18 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/me/jellysquid/mods/sodium/client/render/vertex/VertexFormatDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package me.jellysquid.mods.sodium.client.render.vertex; | ||
|
||
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; | ||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats; | ||
import net.minecraft.client.renderer.vertex.VertexFormat; | ||
import net.minecraft.client.renderer.vertex.VertexFormatElement; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.StampedLock; | ||
|
||
/** | ||
* Inspired by modern Sodium's VertexFormatDescription. | ||
*/ | ||
public class VertexFormatDescription { | ||
public enum Element { | ||
POSITION(DefaultVertexFormats.POSITION_3F), | ||
COLOR(DefaultVertexFormats.COLOR_4UB), | ||
TEXTURE(DefaultVertexFormats.TEX_2F), | ||
NORMAL(DefaultVertexFormats.NORMAL_3B); | ||
|
||
final VertexFormatElement underlyingElement; | ||
static final Map<VertexFormatElement, Element> VANILLA_TO_COMMON = new Reference2ReferenceOpenHashMap<>(); | ||
Element(VertexFormatElement baseElement) { | ||
this.underlyingElement = baseElement; | ||
} | ||
|
||
static { | ||
for(Element e : Element.values()) { | ||
VANILLA_TO_COMMON.put(e.underlyingElement, e); | ||
} | ||
} | ||
} | ||
|
||
private static final Element[] COMMON_ELEMENTS = Element.values(); | ||
|
||
private final int[] elementOffsets; | ||
private final VertexFormat format; | ||
|
||
private static final Map<VertexFormat, VertexFormatDescription> REGISTRY = new Reference2ReferenceOpenHashMap<>(); | ||
private static final StampedLock LOCK = new StampedLock(); | ||
|
||
VertexFormatDescription(VertexFormat format) { | ||
this.elementOffsets = new int[COMMON_ELEMENTS.length]; | ||
Arrays.fill(this.elementOffsets, -1); | ||
this.format = format; | ||
for(int i = 0; i < format.getElementCount(); i++) { | ||
Element commonElement = Element.VANILLA_TO_COMMON.get(format.getElement(i)); | ||
if(commonElement != null) { | ||
elementOffsets[commonElement.ordinal()] = format.getOffset(i) / 4; | ||
} | ||
} | ||
} | ||
|
||
public static VertexFormatDescription get(VertexFormat format) { | ||
long stamp = LOCK.readLock(); | ||
VertexFormatDescription desc; | ||
try { | ||
desc = REGISTRY.get(format); | ||
} finally { | ||
LOCK.unlockRead(stamp); | ||
} | ||
|
||
if (desc != null) { | ||
return desc; | ||
} | ||
|
||
desc = new VertexFormatDescription(format); | ||
|
||
stamp = LOCK.writeLock(); | ||
|
||
try { | ||
REGISTRY.put(format, desc); | ||
} finally { | ||
LOCK.unlockWrite(stamp); | ||
} | ||
|
||
return desc; | ||
} | ||
|
||
public int getIndex(VertexFormatDescription.Element element) { | ||
return this.elementOffsets[element.ordinal()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/me/jellysquid/mods/sodium/mixin/core/pipeline/MixinUnpackedBakedQuad.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.jellysquid.mods.sodium.mixin.core.pipeline; | ||
|
||
import me.jellysquid.mods.sodium.client.model.quad.properties.ModelQuadFlags; | ||
import net.minecraft.client.renderer.block.model.BakedQuad; | ||
import net.minecraftforge.client.model.pipeline.UnpackedBakedQuad; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(UnpackedBakedQuad.class) | ||
public class MixinUnpackedBakedQuad { | ||
protected int cachedFlags; | ||
@Inject(method = "<init>", at = @At("RETURN")) | ||
private void calculateFlags(CallbackInfo ci) { | ||
this.cachedFlags = ModelQuadFlags.getQuadFlags((BakedQuad) (Object) this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters