This commit is contained in:
mcrcortex
2025-06-16 20:17:44 +10:00
parent 1575d7319c
commit 341119386a
2 changed files with 11 additions and 23 deletions

View File

@@ -69,12 +69,12 @@ public class HiZBuffer {
} }
public void buildMipChain(int srcDepthTex, int width, int height) { public void buildMipChain(int srcDepthTex, int width, int height) {
if (this.width != Integer.highestOneBit(width*2) || this.height != Integer.highestOneBit(height*2)) { if (this.width != Integer.highestOneBit(width) || this.height != Integer.highestOneBit(height)) {
if (this.texture != null) { if (this.texture != null) {
this.texture.free(); this.texture.free();
this.texture = null; this.texture = null;
} }
this.alloc(Integer.highestOneBit(width*2), Integer.highestOneBit(height*2)); this.alloc(Integer.highestOneBit(width), Integer.highestOneBit(height));
} }
glBindVertexArray(RenderService.STATIC_VAO); glBindVertexArray(RenderService.STATIC_VAO);
int boundFB = GL11.glGetInteger(GL_DRAW_FRAMEBUFFER_BINDING); int boundFB = GL11.glGetInteger(GL_DRAW_FRAMEBUFFER_BINDING);
@@ -86,26 +86,19 @@ public class HiZBuffer {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
//System.err.println("SRC: " + GlTexture.getRawTextureType(srcDepthTex) + " DST: " + this.texture.id);
glCopyImageSubData(srcDepthTex, GL_TEXTURE_2D, 0,0,0,0,
this.texture.id, GL_TEXTURE_2D, 0,0,0,0,
width, height, 1);
glBindTextureUnit(0, srcDepthTex); glBindTextureUnit(0, srcDepthTex);
glBindSampler(0, this.sampler); glBindSampler(0, this.sampler);
glUniform1i(0, 0); glUniform1i(0, 0);
int cw = this.width; int cw = this.width;
int ch = this.height; int ch = this.height;
glViewport(0, 0, cw, ch); 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);
glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, i); glViewport(0, 0, cw, ch); cw = Math.max(cw/2, 1); ch = Math.max(ch/2, 1);
glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, i);
this.fb.bind(GL_DEPTH_ATTACHMENT, this.texture, i+1);
cw = Math.max(cw/2, 1); ch = Math.max(ch/2, 1); glViewport(0, 0, cw, ch);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glTextureBarrier(); glTextureBarrier();
glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT|GL_TEXTURE_FETCH_BARRIER_BIT); glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT|GL_TEXTURE_FETCH_BARRIER_BIT);
glTextureParameteri(this.texture.id, GL_TEXTURE_BASE_LEVEL, i);
glTextureParameteri(this.texture.id, GL_TEXTURE_MAX_LEVEL, i);
if (i==0) { if (i==0) {
glBindTextureUnit(0, this.texture.id); glBindTextureUnit(0, this.texture.id);
} }

View File

@@ -131,7 +131,7 @@ bool outsideFrustum() {
} }
bool isCulledByHiz() { bool isCulledByHiz() {
vec2 asize = size * vec2(screenW, screenH); vec2 asize = size * vec2(1024, 512);
float miplevel = log2(max(max(asize.x, asize.y),1)); float miplevel = log2(max(max(asize.x, asize.y),1));
//TODO: make a path for if the miplevel would result in the textureSampler sampling a size of 1 //TODO: make a path for if the miplevel would result in the textureSampler sampling a size of 1
@@ -140,14 +140,12 @@ bool isCulledByHiz() {
miplevel = floor(miplevel)-1; miplevel = floor(miplevel)-1;
miplevel = clamp(miplevel, 0, textureQueryLevels(hizDepthSampler)-1); miplevel = clamp(miplevel, 0, textureQueryLevels(hizDepthSampler)-1);
vec2 midpoint = (maxBB.xy + minBB.xy)*0.5f;
float testAgainst = minBB.z; float testAgainst = minBB.z;
//the *2.0f-1.0f converts from the 0->1 range to -1->1 range that depth is in (not having this causes tighter bounds, but causes culling issues in caves) //the *2.0f-1.0f converts from the 0->1 range to -1->1 range that depth is in (not having this causes tighter bounds, but causes culling issues in caves)
//testAgainst = testAgainst*2.0f-1.0f; //testAgainst = testAgainst*2.0f-1.0f;
int ml = 4;//int(miplevel); int ml = int(miplevel);
ivec2 msize = ivec2(128,64);//textureSize(hizDepthSampler, ml);//max(ivec2(1),ivec2(screenW, screenH)>>ml); ivec2 msize = textureSize(hizDepthSampler, ml);//max(ivec2(1),ivec2(screenW, screenH)>>ml);
ivec2 mxbb = clamp(ivec2(maxBB.xy*msize), ivec2(0), msize); ivec2 mxbb = clamp(ivec2(maxBB.xy*msize), ivec2(0), msize);
ivec2 mnbb = clamp(ivec2(minBB.xy*msize), ivec2(0), msize); ivec2 mnbb = clamp(ivec2(minBB.xy*msize), ivec2(0), msize);
float pointSample = -1.0f; float pointSample = -1.0f;
@@ -173,11 +171,8 @@ bool isCulledByHiz() {
//if ((culled) && node22.lodLevel == 0) { //if ((culled) && node22.lodLevel == 0) {
// printf("HiZ sample point: (%f,%f)@%f against %f, value %f", midpoint.x, midpoint.y, miplevel, minBB.z, textureLod(hizDepthSampler, vec3(0.5f,0.5f, 0.000000001f), 9.0f)); // printf("HiZ sample point: (%f,%f)@%f against %f, value %f", midpoint.x, midpoint.y, miplevel, minBB.z, textureLod(hizDepthSampler, vec3(0.5f,0.5f, 0.000000001f), 9.0f));
//} //}
if (pointSample==0.0f) {
//return false;
}
return pointSample<testAgainst; return pointSample<=testAgainst;
} }