Skip to content

Commit

Permalink
Defer SpriteInfoImage init until rendering starts, fixes #100
Browse files Browse the repository at this point in the history
  • Loading branch information
grondag committed Sep 6, 2020
1 parent 0e21fa9 commit 69b4a95
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public final MutableQuadViewImpl fromVanilla(int[] quadData, int startIndex, boo
return this;
}

@Override
public final MutableQuadViewImpl fromVanilla(BakedQuad quad, RenderMaterial material, Direction cullFace) {
return fromVanilla(quad, material, ModelHelper.toFaceIndex(cullFace));
}
Expand Down Expand Up @@ -283,7 +284,6 @@ private void unmapSprite(int textureIndex) {
/**
* Same as logic in SpriteFinder but can assume sprites are mapped - avoids checks
*/
@SuppressWarnings("resource")
private Sprite findSprite(int textureIndex) {
float u = 0;
float v = 0;
Expand All @@ -293,7 +293,7 @@ private Sprite findSprite(int textureIndex) {
v += spriteFloatV(i, textureIndex);
}

return SpriteInfoTexture.instance().spriteFinder.find(u * 0.25f, v * 0.25f);
return SpriteInfoTexture.spriteFinder().find(u * 0.25f, v * 0.25f);
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/grondag/canvas/apiimpl/mesh/QuadViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public int lightmap(int vertexIndex) {

protected int colorOffset(int vertexIndex, int spriteIndex) {
return spriteIndex == 0 ? vertexIndex * BASE_VERTEX_STRIDE + VERTEX_COLOR
: TEXTURE_OFFSET_MINUS + spriteIndex * TEXTURE_QUAD_STRIDE + vertexIndex * TEXTURE_VERTEX_STRIDE;
: TEXTURE_OFFSET_MINUS + spriteIndex * TEXTURE_QUAD_STRIDE + vertexIndex * TEXTURE_VERTEX_STRIDE;
}

@Override
Expand All @@ -339,15 +339,15 @@ protected final float spriteFloatV(int vertexIndex, int spriteIndex) {
@Override
public float spriteU(int vertexIndex, int spriteIndex) {
return isSpriteUnmapped(spriteIndex)
? SpriteInfoTexture.instance().mapU(spriteId(spriteIndex), spriteFloatU(vertexIndex, spriteIndex))
: spriteFloatU(vertexIndex, spriteIndex);
? SpriteInfoTexture.mapU(spriteId(spriteIndex), spriteFloatU(vertexIndex, spriteIndex))
: spriteFloatU(vertexIndex, spriteIndex);
}

@Override
public float spriteV(int vertexIndex, int spriteIndex) {
return isSpriteUnmapped(spriteIndex)
? SpriteInfoTexture.instance().mapV(spriteId(spriteIndex), spriteFloatV(vertexIndex, spriteIndex))
: spriteFloatV(vertexIndex, spriteIndex);
? SpriteInfoTexture.mapV(spriteId(spriteIndex), spriteFloatV(vertexIndex, spriteIndex))
: spriteFloatV(vertexIndex, spriteIndex);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.minecraft.util.math.Matrix4f;

import javax.annotation.Nullable;

import java.util.Random;
import java.util.function.Consumer;

Expand Down Expand Up @@ -100,7 +101,7 @@ void mapMaterials(MutableQuadViewImpl quad) {
return;
}

final Sprite sprite = materialMap.needsSprite() ? SpriteInfoTexture.instance().fromId(quad.spriteId(0)) : null;
final Sprite sprite = materialMap.needsSprite() ? SpriteInfoTexture.fromId(quad.spriteId(0)) : null;
final RenderMaterial mapped = materialMap.getMapped(sprite);

if (mapped != null) {
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/grondag/canvas/buffer/encoding/EncoderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ static void bufferQuadDirect1(MutableQuadViewImpl quad, AbstractRenderContext co
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int[] appendData = context.appendData;
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

assert mat.blendMode() != BlendMode.DEFAULT;

Expand All @@ -504,7 +503,7 @@ static void bufferQuadDirect1(MutableQuadViewImpl quad, AbstractRenderContext co
transformedNormal = normalMatrix.canvas_transform(packedNormal);
}

final int spriteIdCoord = spriteInfo.coordinate(quad.spriteId(0));
final int spriteIdCoord = SpriteInfoTexture.coordinate(quad.spriteId(0));

assert spriteIdCoord <= 0xFFFF;

Expand Down Expand Up @@ -547,7 +546,6 @@ static void bufferQuadDirect2(MutableQuadViewImpl quad, AbstractRenderContext co
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int shaderFlags0 = mat0.shaderFlags << 16;
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

final int[] appendData = context.appendData;
final float[] aoData = quad.ao;
Expand Down Expand Up @@ -588,7 +586,7 @@ static void bufferQuadDirect2(MutableQuadViewImpl quad, AbstractRenderContext co
normalAo2 |= aoData == null ? NO_AO_SHADE : ((Math.round(aoData[2] * 254) - 127) << 24);
normalAo3 |= aoData == null ? NO_AO_SHADE : ((Math.round(aoData[3] * 254) - 127) << 24);

final int spriteIdCoord0 = spriteInfo.coordinate(quad.spriteId(0));
final int spriteIdCoord0 = SpriteInfoTexture.coordinate(quad.spriteId(0));

appendData[3] = quad.spriteColor(0, 0);
appendData[4] = quad.spriteBufferU(0, 0) | (quad.spriteBufferV(0, 0) << 16);
Expand Down Expand Up @@ -619,7 +617,7 @@ static void bufferQuadDirect2(MutableQuadViewImpl quad, AbstractRenderContext co
final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;
final int spriteIdCoord1 = spriteInfo.coordinate(quad.spriteId(1));
final int spriteIdCoord1 = SpriteInfoTexture.coordinate(quad.spriteId(1));

appendData[3] = quad.spriteColor(0, 1);
appendData[4] = quad.spriteBufferU(0, 1) | (quad.spriteBufferV(0, 1) << 16);
Expand Down Expand Up @@ -652,7 +650,6 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
final Matrix4fExt matrix = (Matrix4fExt) (Object) context.matrix();
final Matrix3fExt normalMatrix = context.normalMatrix();
final MeshMaterial mat = quad.material().get();
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
Expand Down Expand Up @@ -698,7 +695,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
normalAo2 |= aoData == null ? NO_AO_SHADE : ((Math.round(aoData[2] * 254) - 127) << 24);
normalAo3 |= aoData == null ? NO_AO_SHADE : ((Math.round(aoData[3] * 254) - 127) << 24);

final int spriteIdCoord0 = spriteInfo.coordinate(quad.spriteId(0));
final int spriteIdCoord0 = SpriteInfoTexture.coordinate(quad.spriteId(0));

appendData[3] = quad.spriteColor(0, 0);
appendData[4] = quad.spriteBufferU(0, 0) | (quad.spriteBufferV(0, 0) << 16);
Expand Down Expand Up @@ -729,7 +726,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;
final int spriteIdCoord1 = spriteInfo.coordinate(quad.spriteId(1));
final int spriteIdCoord1 = SpriteInfoTexture.coordinate(quad.spriteId(1));

appendData[3] = quad.spriteColor(0, 1);
appendData[4] = quad.spriteBufferU(0, 1) | (quad.spriteBufferV(0, 1) << 16);
Expand Down Expand Up @@ -760,7 +757,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
final MeshMaterialLayer mat2 = mat.getLayer(2);
final VertexCollectorImpl buff2 = context.collectors.get(mat2);
final int shaderFlags2 = mat2.shaderFlags << 16;
final int spriteIdCoord2 = spriteInfo.coordinate(quad.spriteId(2));
final int spriteIdCoord2 = SpriteInfoTexture.coordinate(quad.spriteId(2));

appendData[3] = quad.spriteColor(0, 2);
appendData[4] = quad.spriteBufferU(0, 2) | (quad.spriteBufferV(0, 2) << 16);
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/grondag/canvas/mixin/MixinSpriteAtlasTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package grondag.canvas.mixin;

import grondag.canvas.mixinterface.SpriteAtlasTextureExt;
import grondag.canvas.mixinterface.SpriteExt;
import grondag.canvas.texture.SpriteInfoTexture;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand All @@ -32,33 +31,24 @@
import java.util.Map;

@Mixin(SpriteAtlasTexture.class)
public class MixinSpriteAtlasTexture implements SpriteAtlasTextureExt {
private final ObjectArrayList<Sprite> spriteIndex = new ObjectArrayList<>();
public class MixinSpriteAtlasTexture {
@Shadow
private Identifier id;
@Shadow
private Map<Identifier, Sprite> sprites;

@Override
public Map<Identifier, Sprite> canvas_spriteMap() {
return sprites;
}

@Override
public ObjectArrayList<Sprite> canvas_spriteIndex() {
return spriteIndex;
}

@Inject(at = @At("RETURN"), method = "upload")
private void afterUpload(SpriteAtlasTexture.Data input, CallbackInfo info) {
if (id.equals(SpriteAtlasTexture.BLOCK_ATLAS_TEX)) {
SpriteInfoTexture.reset(input);
int index = 0;
final ObjectArrayList<Sprite> spriteIndex = new ObjectArrayList<>();

for (final Sprite sprite : sprites.values()) {
spriteIndex.add(sprite);
((SpriteExt) sprite).canvas_id(index++);
}

SpriteInfoTexture.reset(input, spriteIndex, (SpriteAtlasTexture)(Object) this);
}
}
}

This file was deleted.

12 changes: 6 additions & 6 deletions src/main/java/grondag/canvas/render/DrawHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static class SolidHandler extends DrawHandler {
@SuppressWarnings("resource")
@Override
protected void setupInner() {
SpriteInfoTexture.instance().enable();
SpriteInfoTexture.enable();
RenderSystem.enableTexture();
final TextureManager textureManager = MinecraftClient.getInstance().getTextureManager();
textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX);
Expand All @@ -88,7 +88,7 @@ protected void teardownInner() {
RenderSystem.disableCull();
MinecraftClient.getInstance().gameRenderer.getLightmapTextureManager().disable();
RenderSystem.disableFog();
SpriteInfoTexture.instance().disable();
SpriteInfoTexture.disable();

}

Expand All @@ -102,7 +102,7 @@ private static class DecalHandler extends DrawHandler {
@SuppressWarnings("resource")
@Override
protected void setupInner() {
SpriteInfoTexture.instance().enable();
SpriteInfoTexture.enable();
RenderSystem.enableTexture();
final TextureManager textureManager = MinecraftClient.getInstance().getTextureManager();
textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX);
Expand All @@ -128,7 +128,7 @@ protected void teardownInner() {
RenderSystem.disableFog();
RenderSystem.disableBlend();
RenderSystem.depthMask(true);
SpriteInfoTexture.instance().disable();
SpriteInfoTexture.disable();
}
}

Expand All @@ -140,7 +140,7 @@ private static class TranslucentHandler extends DrawHandler {
@SuppressWarnings("resource")
@Override
protected void setupInner() {
SpriteInfoTexture.instance().enable();
SpriteInfoTexture.enable();
RenderSystem.enableTexture();
final TextureManager textureManager = MinecraftClient.getInstance().getTextureManager();
textureManager.bindTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEX);
Expand All @@ -165,7 +165,7 @@ protected void teardownInner() {
RenderSystem.disableFog();
RenderSystem.disableBlend();
RenderSystem.defaultBlendFunc();
SpriteInfoTexture.instance().disable();
SpriteInfoTexture.disable();
}
}
}
18 changes: 8 additions & 10 deletions src/main/java/grondag/canvas/shader/GlShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,14 @@ public String getSource() {
result = StringUtils.replace(result, "#define _CV_HAS_VERTEX_END", "//#define _CV_HAS_VERTEX_END");
}

final SpriteInfoTexture sit = SpriteInfoTexture.instance();

result = StringUtils.replace(result, "#define _CV_SPRITE_INFO_TEXTURE_SIZE 1024", "#define _CV_SPRITE_INFO_TEXTURE_SIZE " + sit.textureSize());
result = StringUtils.replace(result, "#define _CV_ATLAS_WIDTH 1024", "#define _CV_ATLAS_WIDTH " + sit.atlasWidth);
result = StringUtils.replace(result, "#define _CV_ATLAS_HEIGHT 1024", "#define _CV_ATLAS_HEIGHT " + sit.atlasHeight);
result = StringUtils.replace(result, "#define _CV_SPRITE_INFO_TEXTURE_SIZE 1024", "#define _CV_SPRITE_INFO_TEXTURE_SIZE " + SpriteInfoTexture.textureSize());
result = StringUtils.replace(result, "#define _CV_ATLAS_WIDTH 1024", "#define _CV_ATLAS_WIDTH " + SpriteInfoTexture.atlasWidth());
result = StringUtils.replace(result, "#define _CV_ATLAS_HEIGHT 1024", "#define _CV_ATLAS_HEIGHT " + SpriteInfoTexture.atlasHeight());
}

if (context.pass != ShaderPass.SOLID) {
result = StringUtils.replace(result, "#define SHADER_PASS SHADER_PASS_SOLID",
"#define SHADER_PASS SHADER_PASS_" + context.pass.name());
"#define SHADER_PASS SHADER_PASS_" + context.pass.name());
}

if (context.materialContext.isBlock) {
Expand All @@ -255,7 +253,7 @@ public String getSource() {

if (Configurator.fogMode != FogMode.VANILLA && !context.materialContext.isGui) {
result = StringUtils.replace(result, "#define _CV_FOG_CONFIG _CV_FOG_CONFIG_VANILLA",
"#define _CV_FOG_CONFIG _CV_FOG_CONFIG_" + Configurator.fogMode.name());
"#define _CV_FOG_CONFIG _CV_FOG_CONFIG_" + Configurator.fogMode.name());
}

if ((context.pass == ShaderPass.SOLID || context.pass == ShaderPass.DECAL) && Configurator.enableBloom) {
Expand All @@ -272,15 +270,15 @@ public String getSource() {

if (!MinecraftClient.isAmbientOcclusionEnabled()) {
result = StringUtils.replace(result, "#define AO_SHADING_MODE AO_MODE_NORMAL",
"#define AO_SHADING_MODE AO_MODE_" + AoMode.NONE.name());
"#define AO_SHADING_MODE AO_MODE_" + AoMode.NONE.name());
} else if (Configurator.aoShadingMode != AoMode.NORMAL) {
result = StringUtils.replace(result, "#define AO_SHADING_MODE AO_MODE_NORMAL",
"#define AO_SHADING_MODE AO_MODE_" + Configurator.aoShadingMode.name());
"#define AO_SHADING_MODE AO_MODE_" + Configurator.aoShadingMode.name());
}

if (Configurator.diffuseShadingMode != DiffuseMode.NORMAL) {
result = StringUtils.replace(result, "#define DIFFUSE_SHADING_MODE DIFFUSE_MODE_NORMAL",
"#define DIFFUSE_SHADING_MODE DIFFUSE_MODE_" + Configurator.diffuseShadingMode.name());
"#define DIFFUSE_SHADING_MODE DIFFUSE_MODE_" + Configurator.diffuseShadingMode.name());
}

if (CanvasGlHelper.useGpuShader4()) {
Expand Down
Loading

0 comments on commit 69b4a95

Please sign in to comment.