Skip to content

Commit

Permalink
Thread Safety (#19)
Browse files Browse the repository at this point in the history
* Make CubeRenderer & CustomCubeRenderer instanced and use a ThreadLocal
* Misc reduction of required Tessellator ASM and no more static tessellators
* Misc allocation reduction
  • Loading branch information
mitchej123 authored Feb 22, 2024
1 parent 320f7c9 commit ec2e861
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 47 deletions.
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ channel = stable
mappingsVersion = 12

# Defines other MCP mappings for dependency deobfuscation.
remoteMappings = https://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/
remoteMappings = https\://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/

# Select a default username for testing your mod. You can always override this per-run by running
# `./gradlew runClient --username=AnotherPlayer`, or configuring this command in your IDE.
Expand Down Expand Up @@ -61,6 +61,9 @@ gradleTokenModId =
# [DEPRECATED] Mod name replacement token.
gradleTokenModName =

# [DEPRECATED] Mod Group replacement token.
gradleTokenGroupName =

# [DEPRECATED]
# Multiple source files can be defined here by providing a comma-separated list: Class1.java,Class2.java,Class3.java
# public static final String VERSION = "GRADLETOKEN_VERSION";
Expand Down Expand Up @@ -123,7 +126,7 @@ includeWellKnownRepositories = true
usesMavenPublishing = true

# Maven repository to publish the mod to.
# mavenPublishUrl = https://nexus.gtnewhorizons.com/repository/releases/
# mavenPublishUrl = https\://nexus.gtnewhorizons.com/repository/releases/

# Publishing to Modrinth requires you to set the MODRINTH_TOKEN environment variable to your current Modrinth API token.
#
Expand Down Expand Up @@ -187,5 +190,3 @@ curseForgeRelations =
# This is meant to be set in $HOME/.gradle/gradle.properties.
# ideaCheckSpotlessOnBuild = true

# Non-GTNH properties
gradleTokenGroupName =
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pluginManagement {
}

plugins {
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.8'
id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.14'
}


53 changes: 28 additions & 25 deletions src/main/java/com/enderio/core/client/render/CubeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,44 @@

public final class CubeRenderer {

public static final Vector3d[] verts = new Vector3d[8];
static {
private static final ThreadLocal<CubeRenderer> instance = ThreadLocal.withInitial(CubeRenderer::new);
public final Vector3d[] verts = new Vector3d[8];

public CubeRenderer() {
for (int i = 0; i < verts.length; i++) {
verts[i] = new Vector3d();
}
}

public static void render(Block block, int meta) {
public static CubeRenderer get() {
return instance.get();
}

public void render(Block block, int meta) {
render(block, meta, null);
}

public static void render(Block block, int meta, VertexTransform xForm) {
public void render(Block block, int meta, VertexTransform xForm) {
IIcon[] icons = new IIcon[6];
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
icons[dir.ordinal()] = block.getIcon(dir.ordinal(), meta);
}
render(BoundingBox.UNIT_CUBE.translate(0, -0.1f, 0), icons, xForm, true);
}

public static void render(BoundingBox bb, IIcon tex) {
public void render(BoundingBox bb, IIcon tex) {
render(bb, tex, null, false);
}

public static void render(BoundingBox bb, IIcon tex, boolean tintSides) {
public void render(BoundingBox bb, IIcon tex, boolean tintSides) {
render(bb, tex, null, tintSides);
}

public static void render(BoundingBox bb, IIcon tex, VertexTransform xForm) {
public void render(BoundingBox bb, IIcon tex, VertexTransform xForm) {
render(bb, tex.getMinU(), tex.getMaxU(), tex.getMinV(), tex.getMaxV(), xForm, false);
}

public static void render(BoundingBox bb, IIcon tex, VertexTransform xForm, float[] brightnessPerSide,
boolean tintSides) {
public void render(BoundingBox bb, IIcon tex, VertexTransform xForm, float[] brightnessPerSide, boolean tintSides) {
float minU = 0;
float minV = 0;
float maxU = 1;
Expand All @@ -56,7 +61,7 @@ public static void render(BoundingBox bb, IIcon tex, VertexTransform xForm, floa
render(bb, minU, maxU, minV, maxV, xForm, brightnessPerSide, tintSides);
}

public static void render(BoundingBox bb, IIcon tex, VertexTransform xForm, boolean tintSides) {
public void render(BoundingBox bb, IIcon tex, VertexTransform xForm, boolean tintSides) {
float minU = 0;
float minV = 0;
float maxU = 1;
Expand All @@ -70,19 +75,19 @@ public static void render(BoundingBox bb, IIcon tex, VertexTransform xForm, bool
render(bb, minU, maxU, minV, maxV, xForm, tintSides);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, boolean tintSides) {
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, boolean tintSides) {
render(bb, minU, maxU, minV, maxV, null, tintSides);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV) {
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV) {
render(bb, minU, maxU, minV, maxV, null, false);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm) {
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm) {
render(bb, minU, maxU, minV, maxV, xForm, false);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
boolean tintSides) {
float[] brightnessPerSide = null;
if (tintSides) {
Expand All @@ -94,7 +99,7 @@ public static void render(BoundingBox bb, float minU, float maxU, float minV, fl
render(bb, minU, maxU, minV, maxV, xForm, brightnessPerSide);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
float[] brightnessPerSide, boolean tintSides) {

if (tintSides) {
Expand All @@ -110,7 +115,7 @@ public static void render(BoundingBox bb, float minU, float maxU, float minV, fl
render(bb, minU, maxU, minV, maxV, xForm, brightnessPerSide);
}

public static void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
public void render(BoundingBox bb, float minU, float maxU, float minV, float maxV, VertexTransform xForm,
float[] brightnessPerSide) {

if (brightnessPerSide != null && brightnessPerSide.length != 6) {
Expand Down Expand Up @@ -186,7 +191,7 @@ public static void render(BoundingBox bb, float minU, float maxU, float minV, fl
addVecWithUV(verts[3], minU, maxV);
}

public static void render(BoundingBox bb, IIcon[] icons, boolean tintSides) {
public void render(BoundingBox bb, IIcon[] icons, boolean tintSides) {
float[] brightnessPerSide = null;
if (tintSides) {
brightnessPerSide = new float[6];
Expand All @@ -198,7 +203,7 @@ public static void render(BoundingBox bb, IIcon[] icons, boolean tintSides) {

}

public static void render(BoundingBox bb, IIcon[] icons, VertexTransform xForm, boolean tintSides) {
public void render(BoundingBox bb, IIcon[] icons, VertexTransform xForm, boolean tintSides) {
float[] brightnessPerSide = null;
if (tintSides) {
brightnessPerSide = new float[6];
Expand All @@ -209,15 +214,15 @@ public static void render(BoundingBox bb, IIcon[] icons, VertexTransform xForm,
render(bb, icons, xForm, brightnessPerSide);
}

public static void render(BoundingBox bb, IIcon[] faceTextures, VertexTransform xForm, float[] brightnessPerSide) {
public void render(BoundingBox bb, IIcon[] faceTextures, VertexTransform xForm, float[] brightnessPerSide) {
setupVertices(bb, xForm);
float minU;
float maxU;
float minV;
float maxV;
IIcon tex;

Tessellator tessellator = Tessellator.instance;
final Tessellator tessellator = Tessellator.instance;

tessellator.setNormal(0, 0, -1);
if (brightnessPerSide != null) {
Expand Down Expand Up @@ -311,11 +316,11 @@ public static void render(BoundingBox bb, IIcon[] faceTextures, VertexTransform
addVecWithUV(verts[3], minU, minV);
}

public static void setupVertices(BoundingBox bound) {
public void setupVertices(BoundingBox bound) {
setupVertices(bound, null);
}

public static void setupVertices(BoundingBox bound, VertexTransform xForm) {
public void setupVertices(BoundingBox bound, VertexTransform xForm) {
verts[0].set(bound.minX, bound.minY, bound.minZ);
verts[1].set(bound.maxX, bound.minY, bound.minZ);
verts[2].set(bound.maxX, bound.maxY, bound.minZ);
Expand All @@ -332,10 +337,8 @@ public static void setupVertices(BoundingBox bound, VertexTransform xForm) {
}
}

public static void addVecWithUV(Vector3d vec, double u, double v) {
public void addVecWithUV(Vector3d vec, double u, double v) {
Tessellator.instance.addVertexWithUV(vec.x, vec.y, vec.z, u, v);
}

private CubeRenderer() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

public class CustomCubeRenderer {

public static final CustomCubeRenderer instance = new CustomCubeRenderer();
private static final ThreadLocal<CustomCubeRenderer> instance = ThreadLocal.withInitial(CustomCubeRenderer::new);

public static CustomCubeRenderer get() {
return instance.get();
}

private CustomRenderBlocks rb = null;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/enderio/core/client/render/IconUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static interface IIconProvider {
public int getTextureType();
}

private static ArrayList<IIconProvider> iconProviders = new ArrayList<IIconProvider>();
private static ArrayList<IIconProvider> iconProviders = new ArrayList<>();

public static IIcon whiteTexture;
public static IIcon blankTexture;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/enderio/core/client/render/RenderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ public static int setTesselatorBrightness(IBlockAccess world, int x, int y, int
Block block = world.getBlock(x, y, z);
int res = block == null ? world.getLightBrightnessForSkyBlocks(x, y, z, 0)
: block.getMixedBrightnessForBlock(world, x, y, z);
Tessellator.instance.setBrightness(res);
Tessellator.instance.setColorRGBA_F(1, 1, 1, 1);
final Tessellator tessellator = Tessellator.instance;
tessellator.setBrightness(res);
tessellator.setColorRGBA_F(1, 1, 1, 1);
return res;
}

public static void renderQuad2D(double x, double y, double z, double width, double height, int colorRGB) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDisable(GL11.GL_TEXTURE_2D);
Tessellator tessellator = Tessellator.instance;
final Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.setColorOpaque_I(colorRGB);
tessellator.addVertex(x, y + height, z);
Expand All @@ -279,7 +280,7 @@ public static void renderQuad2D(double x, double y, double z, double width, doub
public static void renderQuad2D(double x, double y, double z, double width, double height, Vector4f colorRGBA) {
GL11.glColor4f(colorRGBA.x, colorRGBA.y, colorRGBA.z, colorRGBA.w);
GL11.glDisable(GL11.GL_TEXTURE_2D);
Tessellator tessellator = Tessellator.instance;
final Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertex(x, y + height, z);
tessellator.addVertex(x + width, y + height, z);
Expand Down Expand Up @@ -706,7 +707,7 @@ public static void renderBackground(FontRenderer fnt, String toRender, Vector4f
float width = fnt.getStringWidth(toRender);
float height = fnt.FONT_HEIGHT;
float padding = 2f;
Tessellator tessellator = Tessellator.instance;
final Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.setColorRGBA_F(color.x, color.y, color.z, color.w);
tessellator.addVertex(-padding, -padding, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

public class SimpleModelRenderer implements ISimpleBlockRenderingHandler {

private final Tessellator tes = Tessellator.instance;

private final WavefrontObject model;

private final int renderId;
Expand All @@ -29,6 +27,7 @@ public int getRenderId() {

@Override
public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) {
final Tessellator tes = Tessellator.instance;
RenderHelper.disableStandardItemLighting();
tes.startDrawingQuads();
tes.setColorOpaque_F(1, 1, 1);
Expand All @@ -40,6 +39,7 @@ public void renderInventoryBlock(Block block, int metadata, int modelId, RenderB
@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId,
RenderBlocks renderer) {
final Tessellator tes = Tessellator.instance;
tes.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(x + .5F, y + .5F, z + .5F);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/enderio/core/client/render/TechneUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class TechneUtil {

private static final TechneModelLoader modelLoader = new TechneModelLoader();

private static final Tessellator tes = Tessellator.instance;

public static List<GroupObject> bakeModel(ModelRenderer model, TechneModel supermodel) {
return bakeModel(model, supermodel, 1);
}
Expand Down Expand Up @@ -352,6 +350,7 @@ public static void renderInventoryBlock(Collection<GroupObject> model, Block blo

public static void renderInventoryBlock(Collection<GroupObject> model, IIcon icon, Block block, int metadata,
RenderBlocks rb) {
final Tessellator tes = Tessellator.instance;
tes.startDrawingQuads();
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(0, -0.47f, 0);
Expand All @@ -363,6 +362,7 @@ public static void renderInventoryBlock(Collection<GroupObject> model, IIcon ico

public static void renderInventoryBlock(GroupObject model, Block block, int metadata, RenderBlocks rb) {
IIcon icon = getIconFor(block, metadata);
final Tessellator tes = Tessellator.instance;
tes.startDrawingQuads();
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(0, -0.47f, 0);
Expand All @@ -383,6 +383,7 @@ public static boolean renderWorldBlock(Collection<GroupObject> model, IIcon icon
if (icon == null) {
return false;
}
final Tessellator tes = Tessellator.instance;
tes.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(x + .5F, y + 0.0375f, z + .5F);
Expand All @@ -395,6 +396,7 @@ public static boolean renderWorldBlock(Collection<GroupObject> model, IIcon icon
public static boolean renderWorldBlock(GroupObject model, IBlockAccess world, int x, int y, int z, Block block,
RenderBlocks rb) {
IIcon icon = getIconFor(block, world, x, y, z);
final Tessellator tes = Tessellator.instance;
tes.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(x + .5F, y + 0.0375f, z + .5F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public VertexTransformComposite(VertexTransform... xforms) {

@Override
public void apply(Vertex vertex) {
for (VertexTransform xform : xforms) {
xform.apply(vertex);
for (int i = 0; i < xforms.length; i++) {
xforms[i].apply(vertex);
}
}

@Override
public void apply(Vector3d vec) {
for (VertexTransform xform : xforms) {
xform.apply(vec);
for (int i = 0; i < xforms.length; i++) {
xforms[i].apply(vec);
}
}

@Override
public void applyToNormal(Vector3f vec) {
for (VertexTransform xform : xforms) {
xform.applyToNormal(vec);
for (int i = 0; i < xforms.length; i++) {
xforms[i].applyToNormal(vec);
}
}

Expand Down

0 comments on commit ec2e861

Please sign in to comment.