Skip to content

Commit

Permalink
Added normal and lightmap buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Nov 24, 2024
1 parent b4fc0a4 commit 8c4cda6
Show file tree
Hide file tree
Showing 54 changed files with 876 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import foundry.veil.impl.client.imgui.VeilImGuiImpl;
import foundry.veil.impl.client.render.pipeline.VeilUniformBlockState;
import foundry.veil.impl.client.render.shader.ShaderProgramImpl;
import foundry.veil.impl.client.render.shader.SimpleShaderProcessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -191,7 +192,6 @@ public final class VeilRenderSystem {
private static ResourceLocation shaderLocation;
private static VertexBuffer vbo;


private VeilRenderSystem() {
}

Expand Down Expand Up @@ -715,6 +715,7 @@ public static void endFrame() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); // Manual unbind to restore the default mc state

UNIFORM_BLOCK_STATE.clear();
SimpleShaderProcessor.clear();
}

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

public enum DynamicBufferType {
ALBEDO("albedo", "Albedo", GlslTypeSpecifier.BuiltinType.VEC4, FramebufferAttachmentDefinition.Format.RGBA8),
NORMAL("normal", "Normal", GlslTypeSpecifier.BuiltinType.IVEC3, FramebufferAttachmentDefinition.Format.RGB8_SNORM),
LIGHT_COLOR("light_color", "LightColor", GlslTypeSpecifier.BuiltinType.VEC3, FramebufferAttachmentDefinition.Format.RGB8),
LIGHT_UV("light_uv", "LightUv", GlslTypeSpecifier.BuiltinType.UVEC2, FramebufferAttachmentDefinition.Format.RG8UI),
NORMAL("normal", "Normal", GlslTypeSpecifier.BuiltinType.VEC4, FramebufferAttachmentDefinition.Format.RGB8_SNORM),
LIGHT_UV("light_uv", "LightUV", GlslTypeSpecifier.BuiltinType.VEC4, FramebufferAttachmentDefinition.Format.RG8),
LIGHT_COLOR("light_color", "LightColor", GlslTypeSpecifier.BuiltinType.VEC4, FramebufferAttachmentDefinition.Format.RGB8),
DEBUG("debug", "Debug", GlslTypeSpecifier.BuiltinType.VEC4, FramebufferAttachmentDefinition.Format.RGBA16F);

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.mojang.blaze3d.vertex.VertexFormat;
import foundry.veil.Veil;
import foundry.veil.api.client.render.VeilRenderSystem;
import foundry.veil.api.client.render.dynamicbuffer.DynamicBufferType;
Expand Down Expand Up @@ -579,6 +580,16 @@ public void addInclude(ResourceLocation name) {
public boolean isSourceFile() {
return this.sourceFile;
}

@Override
public @Nullable String shaderInstance() {
return null;
}

@Override
public @Nullable VertexFormat vertexFormat() {
return null;
}
}

private record ReloadState(Map<ResourceLocation, ProgramDefinition> definitions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foundry.veil.api.client.render.shader.processor;

import com.mojang.blaze3d.vertex.VertexFormat;
import foundry.veil.api.client.render.shader.definition.ShaderPreDefinitions;
import foundry.veil.api.client.render.shader.program.ProgramDefinition;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -116,12 +117,24 @@ interface Context {
@Nullable
ProgramDefinition definition();

/**
* @return The name of the shader instance this was compiled with or <code>null</code> if not a vanilla shader
*/
@Nullable
String shaderInstance();

/**
* @return The set of pre-definitions for shaders
*/
@Nullable
ShaderPreDefinitions preDefinitions();

/**
* @return The vertex format specified in the shader or <code>null</code> if a vanilla shader
*/
@Nullable
VertexFormat vertexFormat();

/**
* @return The OpenGL type of the compiling shader
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package foundry.veil.ext;

import net.minecraft.resources.ResourceLocation;

import java.util.Collection;

public interface ShaderInstanceExtension {

void setActiveBuffers(int activeBuffers);
void veil$recompile(boolean vertex, String source);

Collection<ResourceLocation> veil$getShaderSources();
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
package foundry.veil.impl.client.render.dynamicbuffer;

import com.google.common.collect.Sets;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.preprocessor.GlslPreprocessor;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.VertexFormat;
import foundry.veil.Veil;
import foundry.veil.api.client.render.VeilRenderSystem;
import foundry.veil.api.client.render.dynamicbuffer.DynamicBufferType;
import foundry.veil.api.client.render.framebuffer.AdvancedFbo;
import foundry.veil.ext.ShaderInstanceExtension;
import foundry.veil.impl.client.render.shader.SimpleShaderProcessor;
import foundry.veil.mixin.accessor.GameRendererAccessor;
import net.minecraft.FileUtil;
import net.minecraft.ReportedException;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceProvider;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.NativeResource;

import java.io.IOException;
import java.io.Reader;
import java.nio.IntBuffer;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.lwjgl.opengl.GL11C.*;
import static org.lwjgl.opengl.GL12C.*;
Expand Down Expand Up @@ -45,6 +61,8 @@ public class DynamicBufferManger implements NativeResource {
private final int[] clearBuffers;
private final Map<ResourceLocation, AdvancedFbo> framebuffers;
private final EnumMap<DynamicBufferType, DynamicBuffer> dynamicBuffers;
private final AtomicBoolean vanillaCancelled;
private CompletableFuture<?> vanillaLoadFuture;

public DynamicBufferManger(int width, int height) {
this.activeBuffers = 0;
Expand All @@ -62,6 +80,129 @@ public DynamicBufferManger(int width, int height) {
this.dynamicBuffers.put(value, buffer);
}
}

this.vanillaCancelled = new AtomicBoolean(false);
this.vanillaLoadFuture = CompletableFuture.completedFuture(null);
}

private void deleteFramebuffers() {
for (Map.Entry<ResourceLocation, AdvancedFbo> entry : this.framebuffers.entrySet()) {
entry.getValue().free();
VeilRenderSystem.renderer().getFramebufferManager().removeFramebuffer(entry.getKey());
}
this.framebuffers.clear();
}

/**
* Attempts to preload all vanilla minecraft shader files before creating the shaders on the CPU.
*
* @param shaders The shaders to reload
* @return A future for when vanilla shaders have reloaded
*/
public CompletableFuture<?> reloadVanillaShaders(Collection<ShaderInstance> shaders) {
this.vanillaCancelled.set(true);
return this.vanillaLoadFuture = CompletableFuture.runAsync(() -> {
this.vanillaCancelled.set(false);
ResourceManager resourceManager = Minecraft.getInstance().getResourceManager();

Veil.LOGGER.info("Compiling {} vanilla shaders", shaders.size());
SimpleShaderProcessor.setup(resourceManager);

Map<String, ShaderInstance> shaderMap = new HashMap<>(shaders.size());
for (ShaderInstance shader : shaders) {
shaderMap.put(shader.getName(), shader);
}

while (!shaderMap.isEmpty()) {
if (this.vanillaCancelled.get()) {
return;
}

Set<String> lastFrameShaders = SimpleShaderProcessor.getLastFrameShaders();
boolean compiled = false;

for (String lastFrameShader : lastFrameShaders) {
ShaderInstance shader = shaderMap.remove(lastFrameShader);
if (shader != null) {
if (!shader.getName().startsWith("rendertype_")) {
continue;
}

this.compileShader(resourceManager, shader);
compiled = true;
break;
}
}

if (compiled) {
continue;
}

Iterator<ShaderInstance> iterator = shaderMap.values().iterator();
while (iterator.hasNext()) {
ShaderInstance shader = iterator.next();
iterator.remove();
if (!shader.getName().startsWith("rendertype_")) {
continue;
}

this.compileShader(resourceManager, shader);
break;
}
}

SimpleShaderProcessor.free();
if (!this.vanillaCancelled.get()) {
Veil.LOGGER.info("Compiled {} vanilla shaders", shaders.size());
}
}, Util.backgroundExecutor());
}

private void compileShader(ResourceProvider resourceProvider, ShaderInstance shader) {
ShaderInstanceExtension extension = (ShaderInstanceExtension) shader;
Collection<ResourceLocation> shaderSources = extension.veil$getShaderSources();
VertexFormat vertexFormat = shader.getVertexFormat();
for (ResourceLocation path : shaderSources) {
try (Reader reader = resourceProvider.openAsReader(path)) {
String source = IOUtils.toString(reader);
GlslPreprocessor preprocessor = new GlslPreprocessor() {
private final Set<String> importedPaths = Sets.newHashSet();

@Override
public String applyImport(boolean useFullPath, String directory) {
directory = FileUtil.normalizeResourcePath((useFullPath ? path.getPath() : "shaders/include/") + directory);
if (!this.importedPaths.add(directory)) {
return null;
} else {
ResourceLocation resourcelocation = ResourceLocation.parse(directory);

try {
String s2;
try (Reader reader = resourceProvider.openAsReader(resourcelocation)) {
s2 = IOUtils.toString(reader);
}

return s2;
} catch (IOException e) {
Veil.LOGGER.error("Could not open GLSL import {}: {}", directory, e.getMessage());
return "#error " + e.getMessage();
}
}
}
};
source = String.join("", preprocessor.process(source));

boolean vertex = path.getPath().endsWith(".vsh");
String processed = SimpleShaderProcessor.modify(shader.getName(), path, vertexFormat, vertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER, source);
RenderSystem.recordRenderCall(() -> extension.veil$recompile(vertex, processed));
} catch (Throwable t) {
Veil.LOGGER.error("Couldn't load vanilla shader from {}", path, t);
}
}
}

public boolean isCompilingShaders() {
return !this.vanillaLoadFuture.isDone();
}

public int getActiveBuffers() {
Expand All @@ -78,14 +219,13 @@ public boolean setActiveBuffers(int activeBuffers) {
}

this.activeBuffers = activeBuffers;
this.reloadVanillaShaders(((GameRendererAccessor) Minecraft.getInstance().gameRenderer).getShaders().values());
this.deleteFramebuffers();

try {
VeilRenderSystem.renderer().getShaderManager().setActiveBuffers(activeBuffers);

for (ShaderInstance shader : ((GameRendererAccessor) Minecraft.getInstance().gameRenderer).getShaders().values()) {

}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -98,14 +238,6 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

private void deleteFramebuffers() {
for (Map.Entry<ResourceLocation, AdvancedFbo> entry : this.framebuffers.entrySet()) {
entry.getValue().free();
VeilRenderSystem.renderer().getFramebufferManager().removeFramebuffer(entry.getKey());
}
this.framebuffers.clear();
}

@Override
public void free() {
this.deleteFramebuffers();
Expand Down Expand Up @@ -196,9 +328,9 @@ public void init(int width, int height) {
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0F);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
GlStateManager._texParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Expand Down
Loading

0 comments on commit 8c4cda6

Please sign in to comment.