This commit is contained in:
mcrcortex
2024-05-09 13:22:29 +10:00
parent 05e8145fa6
commit 7a6669fb7d
6 changed files with 40 additions and 23 deletions

View File

@@ -106,6 +106,8 @@ public class Gl46MeshletsFarWorldRenderer extends AbstractFarWorldRenderer<Gl46M
private void updateUniformBuffer(Gl46MeshletViewport viewport) { private void updateUniformBuffer(Gl46MeshletViewport viewport) {
long ptr = UploadStream.INSTANCE.upload(this.uniformBuffer, 0, this.uniformBuffer.size()); long ptr = UploadStream.INSTANCE.upload(this.uniformBuffer, 0, this.uniformBuffer.size());
//TODO:FIXME remove this.sx/sy/sz and make it based on the viewport!!!
var mat = new Matrix4f(viewport.projection).mul(viewport.modelView); var mat = new Matrix4f(viewport.projection).mul(viewport.modelView);
var innerTranslation = new Vector3f((float) (viewport.cameraX-(this.sx<<5)), (float) (viewport.cameraY-(this.sy<<5)), (float) (viewport.cameraZ-(this.sz<<5))); var innerTranslation = new Vector3f((float) (viewport.cameraX-(this.sx<<5)), (float) (viewport.cameraY-(this.sy<<5)), (float) (viewport.cameraZ-(this.sz<<5)));
mat.translate(-innerTranslation.x, -innerTranslation.y, -innerTranslation.z); mat.translate(-innerTranslation.x, -innerTranslation.y, -innerTranslation.z);
@@ -121,8 +123,8 @@ public class Gl46MeshletsFarWorldRenderer extends AbstractFarWorldRenderer<Gl46M
innerTranslation.getToAddress(ptr); ptr += 4*3; innerTranslation.getToAddress(ptr); ptr += 4*3;
MemoryUtil.memPutInt(ptr, viewport.frameId++); ptr += 4; MemoryUtil.memPutInt(ptr, viewport.frameId++); ptr += 4;
//Divided by 2 cause hiz is half the size of the viewport //Divided by 2 cause hiz is half the size of the viewport
MemoryUtil.memPutInt(ptr, viewport.width/2); ptr += 4; MemoryUtil.memPutInt(ptr, viewport.width); ptr += 4;
MemoryUtil.memPutInt(ptr, viewport.height/2); ptr += 4; MemoryUtil.memPutInt(ptr, viewport.height); ptr += 4;
} }
@Override @Override

View File

@@ -12,7 +12,12 @@ import static org.lwjgl.opengl.GL30C.*;
import static org.lwjgl.opengl.GL33.glBindSampler; import static org.lwjgl.opengl.GL33.glBindSampler;
import static org.lwjgl.opengl.GL33.glGenSamplers; import static org.lwjgl.opengl.GL33.glGenSamplers;
import static org.lwjgl.opengl.GL33C.glDeleteSamplers; import static org.lwjgl.opengl.GL33C.glDeleteSamplers;
import static org.lwjgl.opengl.GL33C.glSamplerParameteri;
import static org.lwjgl.opengl.GL42C.GL_FRAMEBUFFER_BARRIER_BIT;
import static org.lwjgl.opengl.GL42C.glMemoryBarrier;
import static org.lwjgl.opengl.GL43C.glCopyImageSubData;
import static org.lwjgl.opengl.GL45C.glNamedFramebufferTexture; import static org.lwjgl.opengl.GL45C.glNamedFramebufferTexture;
import static org.lwjgl.opengl.GL45C.glTextureBarrier;
public class HiZBuffer { public class HiZBuffer {
private final Shader hiz = Shader.make() private final Shader hiz = Shader.make()
@@ -36,15 +41,19 @@ public class HiZBuffer {
this.levels -= 3;//Arbitrary size, shinks the max level by alot and saves a significant amount of processing time this.levels -= 3;//Arbitrary size, shinks the max level by alot and saves a significant amount of processing time
// (could probably increase it to be defined by a max meshlet coverage computation thing) // (could probably increase it to be defined by a max meshlet coverage computation thing)
//We do a hack where we assume (which is probably wrong) that we will on average never this.texture = new GlTexture().store(GL_DEPTH_COMPONENT32, this.levels, width, height);
// get a meshlet that uses 0 mipping
this.levels--;
this.texture = new GlTexture().store(GL_DEPTH_COMPONENT32, this.levels, width/2, height/2);
glTextureParameteri(this.texture.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTextureParameteri(this.texture.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(this.texture.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTextureParameteri(this.texture.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(this.texture.id, GL_TEXTURE_COMPARE_MODE, GL_NONE); glTextureParameteri(this.texture.id, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTextureParameteri(this.texture.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTextureParameteri(this.texture.id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(this.texture.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTextureParameteri(this.texture.id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(this.sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameteri(this.sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(this.sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glSamplerParameteri(this.sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(this.sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
@@ -65,20 +74,26 @@ public class HiZBuffer {
glDepthFunc(GL_ALWAYS); glDepthFunc(GL_ALWAYS);
glBindTextureUnit(0, srcDepthTex);
glCopyImageSubData(srcDepthTex, GL_TEXTURE_2D, 0,0,0,0,
this.texture.id, GL_TEXTURE_2D, 0,0,0,0,
width, height, 1);
glBindTextureUnit(0, this.texture.id);
glBindSampler(0, this.sampler); glBindSampler(0, this.sampler);
glUniform1i(0, 0); glUniform1i(0, 0);
int cw = this.width /2; int cw = this.width;
int ch = this.height/2; int ch = this.height;
for (int i = 0; i < this.levels; i++) { for (int i = 0; i < this.levels-1; i++) {
this.fb.bind(GL_DEPTH_ATTACHMENT, this.texture, i);
glViewport(0, 0, cw, ch); cw /= 2; ch /= 2;
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
if (i == 0) {
glBindTextureUnit(0, this.texture.id);
}
glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, i); glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, i);
glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, i+1); glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, i);
this.fb.bind(GL_DEPTH_ATTACHMENT, this.texture, i+1);
cw /= 2; ch /= 2; glViewport(0, 0, cw, ch);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glTextureBarrier();
glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT);
} }
glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, 0); glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, 0);
glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, this.levels-1);//TODO: CHECK IF ITS -1 or -0 glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, this.levels-1);//TODO: CHECK IF ITS -1 or -0

View File

@@ -11,6 +11,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
public class MixinMinecraftClient { public class MixinMinecraftClient {
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resource/PeriodicNotificationManager;<init>(Lnet/minecraft/util/Identifier;Lit/unimi/dsi/fastutil/objects/Object2BooleanFunction;)V", shift = At.Shift.AFTER)) @Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resource/PeriodicNotificationManager;<init>(Lnet/minecraft/util/Identifier;Lit/unimi/dsi/fastutil/objects/Object2BooleanFunction;)V", shift = At.Shift.AFTER))
private void injectRenderDoc(RunArgs args, CallbackInfo ci) { private void injectRenderDoc(RunArgs args, CallbackInfo ci) {
System.load("C:\\Program Files\\RenderDoc\\renderdoc.dll"); //System.load("C:\\Program Files\\RenderDoc\\renderdoc.dll");
} }
} }

View File

@@ -1,5 +1,4 @@
#version 460 core #version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#define VISIBILITY_ACCESS writeonly #define VISIBILITY_ACCESS writeonly
#import <voxy:lod/gl46mesh/bindings.glsl> #import <voxy:lod/gl46mesh/bindings.glsl>
layout(early_fragment_tests) in; layout(early_fragment_tests) in;

View File

@@ -35,8 +35,8 @@ bool testHiZ(PosHeader secPos, AABBHeader aabb) {
minBB = minBB*0.5+0.5; minBB = minBB*0.5+0.5;
maxBB = maxBB*0.5+0.5; maxBB = maxBB*0.5+0.5;
vec2 size = (maxBB.xy - minBB.xy) * vec2(screensize); vec2 size = (maxBB.xy - minBB.xy) * vec2(ivec2(screensize));
float miplevel = ceil(log2(max(size.x, size.y)));//NOTE: the /2 is cause the mipmaps dont include bottom level depth float miplevel = ceil(log2(max(max(size.x, size.y),1)));
float a = textureLod(hizSampler,minBB.xy,miplevel).r; float a = textureLod(hizSampler,minBB.xy,miplevel).r;
float b = textureLod(hizSampler,vec2(minBB.x,maxBB.y),miplevel).r; float b = textureLod(hizSampler,vec2(minBB.x,maxBB.y),miplevel).r;
@@ -63,7 +63,7 @@ void main() {
PosHeader pos = geometryPool[meshletId*MESHLET_SIZE]; PosHeader pos = geometryPool[meshletId*MESHLET_SIZE];
AABBHeader aabb = geometryPool[meshletId*MESHLET_SIZE+1]; AABBHeader aabb = geometryPool[meshletId*MESHLET_SIZE+1];
if (testHiZ(pos, aabb) || true) {//If didnt cull, insert it back into the stream if (testHiZ(pos, aabb)) {//If didnt cull, insert it back into the stream
meshlets[atomicAdd(drawCmd.instanceCount, 1)+fullMeshletCount] = meshletId; meshlets[atomicAdd(drawCmd.instanceCount, 1)+fullMeshletCount] = meshletId;
} }
} }

View File

@@ -1,5 +1,6 @@
#version 450 #version 450
#extension GL_ARB_gpu_shader_int64 : enable #extension GL_ARB_gpu_shader_int64 : enable
#extension GL_ARB_shader_draw_parameters : require
#import <voxy:lod/quad_format.glsl> #import <voxy:lod/quad_format.glsl>
#import <voxy:lod/gl46mesh/bindings.glsl> #import <voxy:lod/gl46mesh/bindings.glsl>
@@ -12,7 +13,7 @@ Quad quad;
bool setupMeshlet() { bool setupMeshlet() {
gl_CullDistance[0] = 1; gl_CullDistance[0] = 1;
//TODO: replace with vertexAttribute that has a divisor of 1 //TODO: replace with vertexAttribute that has a divisor of 1
uint data = meshlets[gl_InstanceID]; uint data = meshlets[gl_InstanceID + gl_BaseInstanceARB];
if (data == uint(-1)) {//Came across a culled meshlet if (data == uint(-1)) {//Came across a culled meshlet
gl_CullDistance[0] = -1; gl_CullDistance[0] = -1;
//Since the primative is culled, dont need to do any more work or set any values as the primative is discarded //Since the primative is culled, dont need to do any more work or set any values as the primative is discarded