-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add visibility buffer fbo attachment - Write instance id to visbuffer - Move instance id in/out from common to impl shaders
- Loading branch information
Showing
9 changed files
with
151 additions
and
14 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
87 changes: 87 additions & 0 deletions
87
...n/src/backend/java/dev/engine_room/flywheel/backend/engine/indirect/VisibilityBuffer.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,87 @@ | ||
package dev.engine_room.flywheel.backend.engine.indirect; | ||
|
||
import org.lwjgl.opengl.GL30; | ||
import org.lwjgl.opengl.GL32; | ||
import org.lwjgl.opengl.GL46; | ||
|
||
import com.mojang.blaze3d.platform.GlStateManager; | ||
|
||
import dev.engine_room.flywheel.backend.FlwBackend; | ||
import dev.engine_room.flywheel.backend.gl.GlTextureUnit; | ||
import it.unimi.dsi.fastutil.ints.IntArraySet; | ||
import it.unimi.dsi.fastutil.ints.IntSet; | ||
import net.minecraft.client.Minecraft; | ||
|
||
public class VisibilityBuffer { | ||
private static final int ATTACHMENT = GL30.GL_COLOR_ATTACHMENT1; | ||
|
||
private final int textureId; | ||
|
||
private int lastWidth = -1; | ||
private int lastHeight = -1; | ||
|
||
private final IntSet attached = new IntArraySet(); | ||
|
||
public VisibilityBuffer() { | ||
textureId = GL32.glGenTextures(); | ||
|
||
GlStateManager._bindTexture(textureId); | ||
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_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 attach() { | ||
// TODO: clear the vis buffer. maybe do this when we read it? | ||
|
||
var mainRenderTarget = Minecraft.getInstance() | ||
.getMainRenderTarget(); | ||
|
||
setupTexture(mainRenderTarget.width, mainRenderTarget.height); | ||
|
||
if (attached.add(mainRenderTarget.frameBufferId)) { | ||
GL46.glNamedFramebufferTexture(mainRenderTarget.frameBufferId, ATTACHMENT, textureId, 0); | ||
|
||
try { | ||
mainRenderTarget.checkStatus(); | ||
} catch (Exception e) { | ||
FlwBackend.LOGGER.error("Error attaching visbuffer", e); | ||
} | ||
} | ||
|
||
// Enable writes | ||
GL46.glNamedFramebufferDrawBuffers(mainRenderTarget.frameBufferId, new int[] { GL30.GL_COLOR_ATTACHMENT0, ATTACHMENT }); | ||
} | ||
|
||
public void detach() { | ||
var mainRenderTarget = Minecraft.getInstance() | ||
.getMainRenderTarget(); | ||
|
||
// Disable writes | ||
GL46.glNamedFramebufferDrawBuffers(mainRenderTarget.frameBufferId, new int[] { GL30.GL_COLOR_ATTACHMENT0 }); | ||
} | ||
|
||
public void delete() { | ||
GL32.glDeleteTextures(textureId); | ||
} | ||
|
||
private void setupTexture(int width, int height) { | ||
if (lastWidth == width && lastHeight == height) { | ||
return; | ||
} | ||
|
||
// Need to rebind to all fbos because an attachment becomes incomplete when it's resized | ||
attached.clear(); | ||
|
||
lastWidth = width; | ||
lastHeight = height; | ||
|
||
GlTextureUnit.T0.makeActive(); | ||
GlStateManager._bindTexture(textureId); | ||
|
||
// TODO: DSA texture storage? | ||
GL32.glTexImage2D(GL32.GL_TEXTURE_2D, 0, GL32.GL_R32UI, width, height, 0, GL32.GL_RED_INTEGER, GL32.GL_UNSIGNED_INT, 0); | ||
GlStateManager._bindTexture(0); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
common/src/backend/resources/assets/flywheel/flywheel/internal/indirect/read_visibility.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,25 @@ | ||
layout(local_size_x = 8, local_size_y = 8) in; | ||
|
||
layout(binding = 0) uniform usampler2D visBuffer; | ||
|
||
layout(std430) restrict buffer VisibleFlagBuffer { | ||
uint _flw_visibleFlag[]; | ||
}; | ||
|
||
void main() { | ||
uint instanceID = texelFetch(visBuffer, ivec2(gl_GlobalInvocationID.xy), 0).r; | ||
|
||
// Null instance id. | ||
if (instanceID == 0) { | ||
return; | ||
} | ||
|
||
// Adjust for null to find the actual index. | ||
instanceID = instanceID - 1; | ||
|
||
uint index = instanceID >> 5; | ||
|
||
uint mask = 1u << (instanceID & 31u); | ||
|
||
atomicOr(_flw_visibleFlag[index], mask); | ||
} |
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