-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement hi-z occlusion culling - Generate depth pyramid just before issuing cull dispatches - Currently use raw texel fetches but this may be causing loss - Add _flw_cullData to frame uniforms
- Loading branch information
Showing
8 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
common/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/DepthPyramid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package dev.engine_room.flywheel.backend.engine.indirect; | ||
|
||
import org.lwjgl.opengl.GL32; | ||
import org.lwjgl.opengl.GL46; | ||
|
||
import com.mojang.blaze3d.platform.GlStateManager; | ||
|
||
import dev.engine_room.flywheel.backend.gl.shader.GlProgram; | ||
import dev.engine_room.flywheel.lib.math.MoreMath; | ||
import net.minecraft.client.Minecraft; | ||
|
||
public class DepthPyramid { | ||
private final GlProgram depthReduceProgram; | ||
|
||
public final int pyramidTextureId; | ||
|
||
private int lastWidth = -1; | ||
private int lastHeight = -1; | ||
|
||
public DepthPyramid(GlProgram depthReduceProgram) { | ||
this.depthReduceProgram = depthReduceProgram; | ||
|
||
pyramidTextureId = GL32.glGenTextures(); | ||
|
||
GlStateManager._bindTexture(pyramidTextureId); | ||
GlStateManager._texParameter(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_MIN_FILTER, GL32.GL_NEAREST); | ||
GlStateManager._texParameter(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_MAG_FILTER, GL32.GL_NEAREST); | ||
GlStateManager._texParameter(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_COMPARE_MODE, GL32.GL_NONE); | ||
GlStateManager._texParameter(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_WRAP_S, GL32.GL_CLAMP_TO_EDGE); | ||
GlStateManager._texParameter(GL32.GL_TEXTURE_2D, GL32.GL_TEXTURE_WRAP_T, GL32.GL_CLAMP_TO_EDGE); | ||
|
||
} | ||
|
||
public void generate() { | ||
var mainRenderTarget = Minecraft.getInstance() | ||
.getMainRenderTarget(); | ||
|
||
int width = mainRenderTarget.width; | ||
int height = mainRenderTarget.height; | ||
|
||
int mipLevels = getImageMipLevels(width, height); | ||
|
||
createPyramidMips(mipLevels, width, height); | ||
|
||
int depthBufferId = mainRenderTarget.getDepthTextureId(); | ||
|
||
GlStateManager._bindTexture(depthBufferId); | ||
|
||
GL46.glMemoryBarrier(GL46.GL_FRAMEBUFFER_BARRIER_BIT); | ||
|
||
GL46.glActiveTexture(GL32.GL_TEXTURE1); | ||
|
||
depthReduceProgram.bind(); | ||
|
||
for (int i = 0; i < mipLevels; i++) { | ||
int mipWidth = Math.max(1, width >> i); | ||
int mipHeight = Math.max(1, height >> i); | ||
|
||
int srcTexture = (i == 0) ? depthBufferId : pyramidTextureId; | ||
GL46.glBindTexture(GL32.GL_TEXTURE_2D, srcTexture); | ||
|
||
GL46.glBindImageTexture(0, pyramidTextureId, i, false, 0, GL32.GL_WRITE_ONLY, GL32.GL_R32F); | ||
|
||
depthReduceProgram.setUVec2("imageSize", mipWidth, mipHeight); | ||
depthReduceProgram.setInt("lod", Math.max(0, i - 1)); | ||
|
||
GL46.glDispatchCompute(MoreMath.ceilingDiv(mipWidth, 8), MoreMath.ceilingDiv(mipHeight, 8), 1); | ||
|
||
GL46.glMemoryBarrier(GL46.GL_TEXTURE_FETCH_BARRIER_BIT); | ||
} | ||
} | ||
|
||
public void delete() { | ||
GL32.glDeleteTextures(pyramidTextureId); | ||
} | ||
|
||
private void createPyramidMips(int mipLevels, int width, int height) { | ||
if (lastWidth == width && lastHeight == height) { | ||
return; | ||
} | ||
|
||
lastWidth = width; | ||
lastHeight = height; | ||
|
||
GL32.glBindTexture(GL32.GL_TEXTURE_2D, pyramidTextureId); | ||
|
||
for (int i = 0; i < mipLevels; i++) { | ||
int mipWidth = Math.max(1, width >> (i + 1)); | ||
int mipHeight = Math.max(1, height >> (i + 1)); | ||
|
||
GL32.glTexImage2D(GL32.GL_TEXTURE_2D, i, GL32.GL_R32F, mipWidth, mipHeight, 0, GL32.GL_RED, GL32.GL_FLOAT, 0); | ||
} | ||
} | ||
|
||
private static int getImageMipLevels(int width, int height) { | ||
int result = 1; | ||
|
||
while (width > 2 && height > 2) { | ||
result++; | ||
width /= 2; | ||
height /= 2; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/depth_reduce.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
layout(local_size_x = 8, local_size_y = 8) in; | ||
|
||
layout(binding = 0, r32f) uniform writeonly image2D outImage; | ||
layout(binding = 1) uniform sampler2D inImage; | ||
|
||
uniform uvec2 imageSize; | ||
uniform int lod; | ||
|
||
uniform int useMin = 0; | ||
|
||
void main() { | ||
uvec2 pos = gl_GlobalInvocationID.xy; | ||
|
||
ivec2 samplePos = ivec2(pos) * 2; | ||
|
||
float depth01 = texelFetchOffset(inImage, samplePos, lod, ivec2(0, 1)).r; | ||
float depth11 = texelFetchOffset(inImage, samplePos, lod, ivec2(1, 1)).r; | ||
float depth10 = texelFetchOffset(inImage, samplePos, lod, ivec2(1, 0)).r; | ||
float depth00 = texelFetchOffset(inImage, samplePos, lod, ivec2(0, 0)).r; | ||
|
||
float depth; | ||
if (useMin == 0) { | ||
depth = max(max(depth00, depth01), max(depth10, depth11)); | ||
} else { | ||
depth = min(min(depth00, depth01), min(depth10, depth11)); | ||
} | ||
|
||
imageStore(outImage, ivec2(pos), vec4(depth)); | ||
} |
Oops, something went wrong.