Bilinear interpolation for quad vertex data via geometry shader #12
No files matched your search
+3
@@ -80,6 +80,9 @@ public class DefaultChunkRenderer extends ShaderChunkRenderer {
|
||||
|
||||
var primitiveType = shader.getPrimitiveType();
|
||||
|
||||
// TODO: [VEN][GEO-NEO-AO] Remove Temorary Hack
|
||||
primitiveType = GlPrimitiveType.LINES_ADJACENCY;
|
||||
|
||||
Iterator<ChunkRenderList> iterator = renderLists.iterator(renderPass.isReverseOrder());
|
||||
|
||||
this.currentRenderPass = renderPass;
|
||||
|
||||
+7
-2
@@ -52,12 +52,17 @@ public abstract class ShaderChunkRenderer implements ChunkRenderer {
|
||||
|
||||
GlShader vertShader = ShaderLoader.loadShader(ShaderType.VERTEX,
|
||||
"sodium:" + path + ".vsh", constants);
|
||||
|
||||
|
||||
// TODO: [VEN][GEO-NEO-AO] Proper switching if a geo shader is used or not?
|
||||
GlShader geomShader = ShaderLoader.loadShader(ShaderType.GEOM,
|
||||
"sodium:" + path + ".gsh", constants);
|
||||
|
||||
GlShader fragShader = ShaderLoader.loadShader(ShaderType.FRAGMENT,
|
||||
"sodium:" + path + ".fsh", constants);
|
||||
|
||||
try {
|
||||
var builder = GlProgram.builder("sodium:chunk_shader").attachShader(vertShader).attachShader(fragShader);
|
||||
// TODO: [VEN][GEO-NEO-AO] Proper switching if a geo shader is used or not?
|
||||
var builder = GlProgram.builder("sodium:chunk_shader").attachShader(vertShader).attachShader(geomShader).attachShader(fragShader);
|
||||
int i = 0;
|
||||
for (var attr : options.vertexType().getVertexFormat().getAttributes()) {
|
||||
builder.bindAttribute(attr.getName(), i++);
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package org.embeddedt.embeddium.impl.render.chunk.compile.sorting;
|
||||
|
||||
import org.embeddedt.embeddium.impl.render.chunk.sorting.TranslucentQuadAnalyzer;
|
||||
import org.embeddedt.embeddium.impl.util.sorting.MergeSort;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.BitSet;
|
||||
|
||||
|
||||
// TODO: [VEN][GEO-NEO-AO] There is something I must have missed in this implementation, it causes flickering
|
||||
public class GSQuadPrimitiveType implements ChunkPrimitiveType {
|
||||
public static final GSQuadPrimitiveType INSTANCE = new GSQuadPrimitiveType();
|
||||
|
||||
private static final int ELEMENTS_PER_PRIMITIVE = 4;
|
||||
private static final int VERTICES_PER_PRIMITIVE = 4;
|
||||
|
||||
private static final int FAKE_STATIC_CAMERA_OFFSET = 1000;
|
||||
|
||||
@Override
|
||||
public int getIndexBufferElementsPerPrimitive() {
|
||||
return ELEMENTS_PER_PRIMITIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVerticesPerPrimitive() {
|
||||
return VERTICES_PER_PRIMITIVE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateSimpleIndexBuffer(ByteBuffer indexBuffer, int numPrimitives) {
|
||||
int minimumRequiredBufferSize = getIndexBufferSize(numPrimitives);
|
||||
if(indexBuffer.capacity() < minimumRequiredBufferSize) {
|
||||
throw new IllegalStateException("Given index buffer has length " + indexBuffer.capacity() + " but we need " + minimumRequiredBufferSize);
|
||||
}
|
||||
long ptr = MemoryUtil.memAddress(indexBuffer);
|
||||
|
||||
for (int primitiveIndex = 0; primitiveIndex < numPrimitives; primitiveIndex++) {
|
||||
int indexOffset = primitiveIndex * ELEMENTS_PER_PRIMITIVE;
|
||||
int vertexOffset = primitiveIndex * VERTICES_PER_PRIMITIVE;
|
||||
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateIndexBuffer(ByteBuffer indexBuffer, int[] primitiveMapping) {
|
||||
int bufferSize = getIndexBufferSize(primitiveMapping.length);
|
||||
if(indexBuffer.capacity() != bufferSize) {
|
||||
throw new IllegalStateException("Given index buffer has length " + indexBuffer.capacity() + " but we expected " + bufferSize);
|
||||
}
|
||||
long ptr = MemoryUtil.memAddress(indexBuffer);
|
||||
|
||||
for (int primitiveIndex = 0; primitiveIndex < primitiveMapping.length; primitiveIndex++) {
|
||||
int indexOffset = primitiveIndex * ELEMENTS_PER_PRIMITIVE;
|
||||
|
||||
// Map to the desired primitive
|
||||
int vertexOffset = primitiveMapping[primitiveIndex] * VERTICES_PER_PRIMITIVE;
|
||||
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2);
|
||||
MemoryUtil.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildStaticDistanceArray(float[] centers, float[] distanceArray, float x, float y, float z,
|
||||
float normX, float normY, float normZ, int quadCount, BitSet normalSigns) {
|
||||
for (int quadIdx = 0; quadIdx < quadCount; ++quadIdx) {
|
||||
int centerIdx = quadIdx * 3;
|
||||
|
||||
// Compute distance using projection of vector from camera->quad center onto shared normal, flipped by sign
|
||||
// to accommodate backwards-facing quads in the same plane extensions
|
||||
|
||||
float qX = centers[centerIdx + 0] - x;
|
||||
float qY = centers[centerIdx + 1] - y;
|
||||
float qZ = centers[centerIdx + 2] - z;
|
||||
|
||||
distanceArray[quadIdx] = (normX * qX + normY * qY + normZ * qZ) * (normalSigns.get(quadIdx) ? 1 : -1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void buildDynamicDistanceArray(float[] centers, float[] distanceArray, int quadCount, float x,
|
||||
float y, float z) {
|
||||
// Sort using distance to camera directly
|
||||
for (int quadIdx = 0; quadIdx < quadCount; ++quadIdx) {
|
||||
int centerIdx = quadIdx * 3;
|
||||
|
||||
float qX = centers[centerIdx + 0] - x;
|
||||
float qY = centers[centerIdx + 1] - y;
|
||||
float qZ = centers[centerIdx + 2] - z;
|
||||
distanceArray[quadIdx] = qX * qX + qY * qY + qZ * qZ;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateSortedIndexBuffer(ByteBuffer indexBuffer, int quadCount, @Nullable TranslucentQuadAnalyzer.SortState chunkData, float x, float y, float z) {
|
||||
if (chunkData == null || chunkData.level() == TranslucentQuadAnalyzer.Level.NONE || chunkData.centersLength() < 3) {
|
||||
generateSimpleIndexBuffer(indexBuffer, quadCount);
|
||||
return;
|
||||
}
|
||||
|
||||
if (quadCount != (chunkData.centersLength() / 3)) {
|
||||
throw new IllegalStateException(String.format("Mismatched SortState (%d) vs given quad count (%d)", chunkData.centersLength() / 3, quadCount));
|
||||
}
|
||||
|
||||
float[] centers = chunkData.centers();
|
||||
int[] indicesArray = new int[quadCount];
|
||||
float[] distanceArray = new float[quadCount];
|
||||
boolean isStatic = chunkData.level() == TranslucentQuadAnalyzer.Level.STATIC;
|
||||
for (int quadIdx = 0; quadIdx < quadCount; ++quadIdx) {
|
||||
indicesArray[quadIdx] = quadIdx;
|
||||
}
|
||||
|
||||
if (isStatic) {
|
||||
buildStaticDistanceArray(centers, distanceArray,
|
||||
centers[0] + chunkData.sharedNormal().x * FAKE_STATIC_CAMERA_OFFSET,
|
||||
centers[1] + chunkData.sharedNormal().y * FAKE_STATIC_CAMERA_OFFSET,
|
||||
centers[2] + chunkData.sharedNormal().z * FAKE_STATIC_CAMERA_OFFSET,
|
||||
chunkData.sharedNormal().x,
|
||||
chunkData.sharedNormal().y,
|
||||
chunkData.sharedNormal().z,
|
||||
quadCount,
|
||||
chunkData.normalSigns());
|
||||
} else {
|
||||
buildDynamicDistanceArray(centers, distanceArray, quadCount, x, y, z);
|
||||
}
|
||||
|
||||
MergeSort.mergeSort(indicesArray, distanceArray);
|
||||
|
||||
generateIndexBuffer(indexBuffer, indicesArray);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,13 @@
|
||||
|
||||
#import <sodium:include/fog.glsl>
|
||||
|
||||
in VS_OUT
|
||||
// TODO: [VEN][GEO-NEO-AO] Proper switching if a geo shader is used or not?
|
||||
// in VS_OUT
|
||||
in GS_OUT
|
||||
{
|
||||
vec4 v_Color; // The interpolated vertex color
|
||||
vec2 v_TexCoord; // The interpolated block texture coordinates
|
||||
vec2 edge;
|
||||
flat vec4 v_Color[4];
|
||||
vec2 v_TexCoord;
|
||||
|
||||
float v_MaterialMipBias;
|
||||
#ifdef USE_FRAGMENT_DISCARD
|
||||
@@ -41,15 +44,22 @@ void main() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// TODO: [VEN][GEO-NEO-AO] Proper switching if a geo shader is used or not?
|
||||
|
||||
vec4 c1 = mix(fs_in.v_Color[0], fs_in.v_Color[1], fs_in.edge.x);
|
||||
vec4 c2 = mix(fs_in.v_Color[2], fs_in.v_Color[3], fs_in.edge.x);
|
||||
|
||||
vec4 m_color = mix(c1, c2, fs_in.edge.y);
|
||||
|
||||
#ifdef USE_VANILLA_COLOR_FORMAT
|
||||
// Apply per-vertex color. AO shade is applied ahead of time on the CPU.
|
||||
diffuseColor *= fs_in.v_Color;
|
||||
diffuseColor *= m_color;
|
||||
#else
|
||||
// Apply per-vertex color
|
||||
diffuseColor.rgb *= fs_in.v_Color.rgb;
|
||||
diffuseColor.rgb *= m_color.rgb;
|
||||
|
||||
// Apply ambient occlusion "shade"
|
||||
diffuseColor.rgb *= fs_in.v_Color.a;
|
||||
diffuseColor.rgb *= m_color.a;
|
||||
#endif
|
||||
|
||||
#ifdef USE_FOG
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
#version 330 core
|
||||
|
||||
layout (lines_adjacency) in;
|
||||
layout (triangle_strip, max_vertices = 6) out;
|
||||
|
||||
// TODO: [VEN][GEO-NEO-AO] Cleaner layout definitions?
|
||||
in VS_OUT
|
||||
{
|
||||
vec4 v_Color;// The interpolated vertex color
|
||||
vec2 v_TexCoord;// The interpolated block texture coordinates
|
||||
|
||||
float v_MaterialMipBias;
|
||||
#ifdef USE_FRAGMENT_DISCARD
|
||||
float v_MaterialAlphaCutoff;
|
||||
#endif
|
||||
|
||||
#ifdef USE_FOG
|
||||
float v_FragDistance;// The fragment's distance from the camera
|
||||
#endif
|
||||
} gs_in[4];
|
||||
|
||||
out GS_OUT
|
||||
{
|
||||
vec2 edge;
|
||||
flat vec4 v_Color[4];
|
||||
vec2 v_TexCoord;
|
||||
|
||||
float v_MaterialMipBias;
|
||||
#ifdef USE_FRAGMENT_DISCARD
|
||||
float v_MaterialAlphaCutoff;
|
||||
#endif
|
||||
|
||||
#ifdef USE_FOG
|
||||
float v_FragDistance;// The fragment's distance from the camera
|
||||
#endif
|
||||
} gs_out;
|
||||
|
||||
void setVertex(int i) {
|
||||
// TODO: [VEN][GEO-NEO-AO] These numbers were found by 'guessing'
|
||||
const vec2 edges[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
|
||||
|
||||
gl_Position = gl_in[i].gl_Position;
|
||||
|
||||
gs_out.edge = edges[i];
|
||||
|
||||
gs_out.v_TexCoord = gs_in[i].v_TexCoord;
|
||||
|
||||
gs_out.v_MaterialMipBias = gs_in[i].v_MaterialMipBias;
|
||||
|
||||
#ifdef USE_FRAGMENT_DISCARD
|
||||
gs_out.v_MaterialAlphaCutoff = gs_in[i].v_MaterialAlphaCutoff;
|
||||
#endif
|
||||
|
||||
#ifdef USE_FOG
|
||||
gs_out.v_FragDistance = gs_in[i].v_FragDistance;
|
||||
#endif
|
||||
}
|
||||
|
||||
void main() {
|
||||
setVertex(0);
|
||||
EmitVertex();
|
||||
|
||||
setVertex(1);
|
||||
EmitVertex();
|
||||
|
||||
setVertex(2);
|
||||
gs_out.v_Color[0] = gs_in[0].v_Color;
|
||||
gs_out.v_Color[1] = gs_in[3].v_Color;
|
||||
gs_out.v_Color[2] = gs_in[1].v_Color;
|
||||
gs_out.v_Color[3] = gs_in[2].v_Color;
|
||||
EmitVertex();
|
||||
|
||||
EndPrimitive();
|
||||
|
||||
setVertex(0);
|
||||
EmitVertex();
|
||||
|
||||
setVertex(2);
|
||||
EmitVertex();
|
||||
|
||||
setVertex(3);
|
||||
|
||||
gs_out.v_Color[0] = gs_in[0].v_Color;
|
||||
gs_out.v_Color[1] = gs_in[3].v_Color;
|
||||
gs_out.v_Color[2] = gs_in[1].v_Color;
|
||||
gs_out.v_Color[3] = gs_in[2].v_Color;
|
||||
EmitVertex();
|
||||
|
||||
EndPrimitive();
|
||||
}
|
||||
+2
-2
@@ -3,7 +3,7 @@ package org.taumc.celeritas.impl.render.terrain;
|
||||
import com.google.common.collect.ImmutableListMultimap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.RenderPassConfiguration;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.QuadPrimitiveType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.compile.sorting.GSQuadPrimitiveType;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.Material;
|
||||
import org.embeddedt.embeddium.impl.render.chunk.terrain.material.parameters.AlphaCutoffParameter;
|
||||
@@ -81,7 +81,7 @@ public class ArchaicRenderPassConfigurationBuilder {
|
||||
return new RenderPassConfiguration<>(vertexType,
|
||||
renderTypeToMaterialMap,
|
||||
vanillaRenderStageMap.asMap(),
|
||||
type -> QuadPrimitiveType.INSTANCE,
|
||||
type -> GSQuadPrimitiveType.INSTANCE, // TODO: [VEN][GEO-NEO-AO] Remove Temorary Hack
|
||||
CUTOUT_MIPPED_MATERIAL,
|
||||
CUTOUT_MIPPED_MATERIAL,
|
||||
TRANSLUCENT_MATERIAL);
|
||||
|
||||
Reference in new issue
Block a user