Skip to content

Commit

Permalink
MaterialContext -> EncodingContext, decouple from material state
Browse files Browse the repository at this point in the history
  • Loading branch information
grondag committed Aug 22, 2020
1 parent 995da45 commit c3d80ed
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import grondag.canvas.buffer.encoding.VertexCollectorList;
import grondag.canvas.buffer.encoding.VertexEncoders;
import grondag.canvas.light.AoCalculator;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.material.EncodingContext;
import grondag.canvas.material.MaterialVertexFormats;
import grondag.canvas.mixinterface.Matrix3fExt;
import grondag.frex.api.material.MaterialMap;
Expand Down Expand Up @@ -161,7 +161,7 @@ protected final boolean cullTest(MutableQuadViewImpl quad) {

protected abstract BlockState blockState();

public abstract MaterialContext materialContext();
public abstract EncodingContext materialContext();

public abstract VertexConsumer consumer(MeshMaterialLayer mat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import grondag.canvas.apiimpl.material.MeshMaterialLayer;
import grondag.canvas.light.AoCalculator;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.material.EncodingContext;
import grondag.canvas.mixinterface.Matrix3fExt;

/**
Expand All @@ -41,7 +41,7 @@ public class BlockRenderContext extends AbstractBlockRenderContext<BlockRenderVi
public static ThreadLocal<BlockRenderContext> POOL = ThreadLocal.withInitial(BlockRenderContext::new);

public BlockRenderContext() {
collectors.setContext(MaterialContext.BLOCK);
collectors.setContext(EncodingContext.BLOCK);
}

public static void reload() {
Expand Down Expand Up @@ -121,8 +121,8 @@ public boolean tesselate(BlockModelRenderer vanillaRenderer, BlockRenderView blo
}

@Override
public MaterialContext materialContext() {
return MaterialContext.BLOCK;
public EncodingContext materialContext() {
return EncodingContext.BLOCK;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import grondag.canvas.apiimpl.material.MeshMaterialLayer;
import grondag.canvas.apiimpl.mesh.MutableQuadViewImpl;
import grondag.canvas.light.AoCalculator;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.material.EncodingContext;
import grondag.canvas.mixinterface.Matrix3fExt;
import grondag.canvas.shader.ShaderPass;

Expand All @@ -64,7 +64,7 @@ public class ItemRenderContext extends AbstractRenderContext implements RenderCo

private int lightmap;
private ItemStack itemStack;
private MaterialContext context = MaterialContext.ITEM_GUI;
private final EncodingContext context = EncodingContext.ITEM;


private final Supplier<Random> randomSupplier = () -> {
Expand All @@ -76,34 +76,7 @@ public class ItemRenderContext extends AbstractRenderContext implements RenderCo
public ItemRenderContext(ItemColors colorMap) {
super();
this.colorMap = colorMap;
}

public ItemRenderContext prepare(ModelTransformation.Mode mode) {
switch (mode) {
case NONE:
case THIRD_PERSON_LEFT_HAND:
case THIRD_PERSON_RIGHT_HAND:
case FIRST_PERSON_LEFT_HAND:
case FIRST_PERSON_RIGHT_HAND:
context = MaterialContext.ITEM_HELD;
break;
case FIXED:
context = MaterialContext.ITEM_FIXED;
break;
case GROUND:
context = MaterialContext.ITEM_GROUND;
break;
default:
case GUI:
context = MaterialContext.ITEM_GUI;
break;
case HEAD:
context = MaterialContext.ITEM_HEAD;
break;
}

collectors.setContext(context);
return this;
collectors.setContext(EncodingContext.ITEM);
}

public void renderModel(ItemStack itemStack, Mode transformMode, boolean invert, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int lightmap, int overlay, FabricBakedModel model) {
Expand Down Expand Up @@ -167,7 +140,7 @@ private VertexConsumer quadVertexConsumer(BlendMode blendMode) {
}

@Override
public MaterialContext materialContext() {
public EncodingContext materialContext() {
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
import grondag.canvas.apiimpl.material.MeshMaterialLayer;
import grondag.canvas.light.AoCalculator;
import grondag.canvas.light.LightSmoother;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.material.MaterialState;
import grondag.canvas.material.EncodingContext;
import grondag.canvas.mixinterface.Matrix3fExt;
import grondag.canvas.terrain.FastRenderRegion;
import grondag.canvas.terrain.ProtoRenderRegion;
Expand All @@ -59,7 +58,7 @@ public class TerrainRenderContext extends AbstractBlockRenderContext<FastRenderR

public TerrainRenderContext() {
region = new FastRenderRegion(this);
collectors.setContext(MaterialContext.TERRAIN);
collectors.setContext(EncodingContext.TERRAIN);
}

private final AoCalculator aoCalc = new AoCalculator() {
Expand Down Expand Up @@ -125,13 +124,13 @@ private void tesselateInner(BlockState blockState, BlockPos blockPos, boolean de
}

@Override
public MaterialContext materialContext() {
return MaterialContext.TERRAIN;
public EncodingContext materialContext() {
return EncodingContext.TERRAIN;
}

@Override
public VertexConsumer consumer(MeshMaterialLayer mat) {
return collectors.get(MaterialState.get(MaterialContext.TERRAIN, mat));
return collectors.get(mat);
}

@Override
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/grondag/canvas/buffer/encoding/EncoderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import grondag.canvas.apiimpl.rendercontext.AbstractRenderContext;
import grondag.canvas.apiimpl.util.ColorHelper;
import grondag.canvas.apiimpl.util.NormalHelper;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.mixinterface.Matrix3fExt;
import grondag.canvas.mixinterface.Matrix4fExt;
import grondag.canvas.texture.SpriteInfoTexture;
Expand Down Expand Up @@ -469,7 +468,7 @@ static void bufferQuadDirect1(MutableQuadViewImpl quad, AbstractRenderContext co
final float[] aoData = quad.ao;
final MeshMaterial mat = quad.material().get();
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int[] appendData = context.appendData;
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

Expand Down Expand Up @@ -529,7 +528,7 @@ static void bufferQuadDirect2(MutableQuadViewImpl quad, AbstractRenderContext co
final Matrix3fExt normalMatrix = context.normalMatrix();
final MeshMaterial mat = quad.material().get();
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int shaderFlags0 = mat0.shaderFlags << 16;
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

Expand Down Expand Up @@ -601,7 +600,7 @@ static void bufferQuadDirect2(MutableQuadViewImpl quad, AbstractRenderContext co
buff0.add(appendData, 32);

final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(MaterialContext.TERRAIN, mat1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;
final int spriteIdCoord1 =spriteInfo.coordinate(quad.spriteId(1));

Expand Down Expand Up @@ -639,7 +638,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
final SpriteInfoTexture spriteInfo = SpriteInfoTexture.instance();

final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int shaderFlags0 = mat0.shaderFlags << 16;

final int[] appendData = context.appendData;
Expand Down Expand Up @@ -711,7 +710,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
buff0.add(appendData, 32);

final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(MaterialContext.TERRAIN, mat1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;
final int spriteIdCoord1 = spriteInfo.coordinate(quad.spriteId(1));

Expand Down Expand Up @@ -742,7 +741,7 @@ static void bufferQuadDirect3(MutableQuadViewImpl quad, AbstractRenderContext co
buff1.add(appendData, 32);

final MeshMaterialLayer mat2 = mat.getLayer(2);
final VertexCollectorImpl buff2 = context.collectors.get(MaterialContext.TERRAIN, mat2);
final VertexCollectorImpl buff2 = context.collectors.get(mat2);
final int shaderFlags2 = mat2.shaderFlags << 16;
final int spriteIdCoord2 = spriteInfo.coordinate(quad.spriteId(2));

Expand Down
13 changes: 6 additions & 7 deletions src/main/java/grondag/canvas/buffer/encoding/HdEncoders.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import grondag.canvas.apiimpl.mesh.MutableQuadViewImpl;
import grondag.canvas.apiimpl.rendercontext.AbstractRenderContext;
import grondag.canvas.light.LightmapHd;
import grondag.canvas.material.MaterialContext;
import grondag.canvas.material.MaterialVertexFormats;
import grondag.canvas.mixinterface.Matrix3fExt;
import grondag.canvas.mixinterface.Matrix4fExt;
Expand Down Expand Up @@ -57,7 +56,7 @@ static void bufferQuadHd1(MutableQuadViewImpl quad, AbstractRenderContext contex
final float[] aoData = quad.ao;
final MeshMaterial mat = quad.material().get();
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int[] appendData = context.appendData;

final LightmapHd hdLight = quad.hdLight;
Expand Down Expand Up @@ -117,7 +116,7 @@ static void bufferQuadHd2(MutableQuadViewImpl quad, AbstractRenderContext contex
final Matrix3fExt normalMatrix = context.normalMatrix();
final MeshMaterial mat = quad.material().get();
final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int shaderFlags0 = mat0.shaderFlags << 16;

final int[] appendData = context.appendData;
Expand Down Expand Up @@ -197,7 +196,7 @@ static void bufferQuadHd2(MutableQuadViewImpl quad, AbstractRenderContext contex
buff0.add(appendData, QUAD_STRIDE);

final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(MaterialContext.TERRAIN, mat1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;

appendData[3] = quad.spriteColor(0, 1);
Expand Down Expand Up @@ -230,7 +229,7 @@ static void bufferQuadHd3(MutableQuadViewImpl quad, AbstractRenderContext contex


final MeshMaterialLayer mat0 = mat.getLayer(0);
final VertexCollectorImpl buff0 = context.collectors.get(MaterialContext.TERRAIN, mat0);
final VertexCollectorImpl buff0 = context.collectors.get(mat0);
final int shaderFlags0 = mat0.shaderFlags << 16;

final int[] appendData = context.appendData;
Expand Down Expand Up @@ -309,7 +308,7 @@ static void bufferQuadHd3(MutableQuadViewImpl quad, AbstractRenderContext contex
buff0.add(appendData, QUAD_STRIDE);

final MeshMaterialLayer mat1 = mat.getLayer(1);
final VertexCollectorImpl buff1 = context.collectors.get(MaterialContext.TERRAIN, mat1);
final VertexCollectorImpl buff1 = context.collectors.get(mat1);
final int shaderFlags1 = mat1.shaderFlags << 16;

appendData[3] = quad.spriteColor(0, 1);
Expand All @@ -335,7 +334,7 @@ static void bufferQuadHd3(MutableQuadViewImpl quad, AbstractRenderContext contex
buff1.add(appendData, QUAD_STRIDE);

final MeshMaterialLayer mat2 = mat.getLayer(2);
final VertexCollectorImpl buff2 = context.collectors.get(MaterialContext.TERRAIN, mat2);
final VertexCollectorImpl buff2 = context.collectors.get(mat2);
final int shaderFlags2 = mat2.shaderFlags << 16;

appendData[3] = quad.spriteColor(0, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.util.math.MathHelper;

import grondag.canvas.material.EncodingContext;
import grondag.canvas.material.MaterialState;
import grondag.canvas.material.MaterialVertexFormat;
import grondag.canvas.material.MaterialVertexFormats;
import grondag.fermion.intstream.IntStreamProvider;
import grondag.fermion.intstream.IntStreamProvider.IntStreamImpl;

public class VertexCollectorImpl implements VertexCollector {
private final IntStreamImpl data = INT_STREAM_PROVIDER.claim();
private int integerSize = 0;

/**
* Used for vanilla quads
*/
private VertexEncoder defaultEncoder;

private MaterialState materialState;

private MaterialVertexFormat format;

/**
* Holds per-quad distance after {@link #sortQuads(double, double, double)} is
* called
Expand All @@ -43,9 +49,10 @@ public class VertexCollectorImpl implements VertexCollector {
public VertexCollectorImpl() {
}

public VertexCollectorImpl prepare(MaterialState materialState) {
defaultEncoder = VertexEncoders.getDefault(materialState);
public VertexCollectorImpl prepare(EncodingContext context, MaterialState materialState) {
defaultEncoder = VertexEncoders.getDefault(context, materialState);
this.materialState = materialState;
format = MaterialVertexFormats.get(context, materialState.isTranslucent);
return this;
}

Expand All @@ -71,7 +78,7 @@ public MaterialState materialState() {
}

public int vertexCount() {
return integerSize / materialState.format.vertexStrideInts;
return integerSize / format.vertexStrideInts;
}

public int quadCount() {
Expand Down Expand Up @@ -234,7 +241,7 @@ private void doSort(VertexCollectorImpl caller, double x, double y, double z) {
data = caller.data;

// works because 4 bytes per int
quadIntStride = caller.materialState.format.vertexStrideBytes;
quadIntStride = caller.format.vertexStrideBytes;
final int vertexIntStride = quadIntStride / 4;
final int quadCount = caller.vertexCount() / 4;

Expand Down
Loading

0 comments on commit c3d80ed

Please sign in to comment.