package net.coderbot.iris.shaderpack; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import it.unimi.dsi.fastutil.booleans.BooleanConsumer; import net.coderbot.iris.gl.blending.AlphaTestOverride; import net.coderbot.iris.gl.blending.BlendModeOverride; import net.coderbot.iris.gl.blending.BufferBlendInformation; import net.coderbot.iris.gl.framebuffer.ViewportData; import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; public class ProgramDirectives { private static final ImmutableList LEGACY_RENDER_TARGETS = PackRenderTargetDirectives.LEGACY_RENDER_TARGETS; private final int[] drawBuffers; private final ViewportData viewportScale; @Nullable private final AlphaTestOverride alphaTestOverride; private final Optional blendModeOverride; private final List bufferBlendInformations; private final ImmutableSet mipmappedBuffers; private final ImmutableMap explicitFlips; private ProgramDirectives(int[] drawBuffers, ViewportData viewportScale, @Nullable AlphaTestOverride alphaTestOverride, Optional blendModeOverride, List bufferBlendInformations, ImmutableSet mipmappedBuffers, ImmutableMap explicitFlips) { this.drawBuffers = drawBuffers; this.viewportScale = viewportScale; this.alphaTestOverride = alphaTestOverride; this.blendModeOverride = blendModeOverride; this.bufferBlendInformations = bufferBlendInformations; this.mipmappedBuffers = mipmappedBuffers; this.explicitFlips = explicitFlips; } ProgramDirectives(ProgramSource source, ShaderProperties properties, Set supportedRenderTargets, @Nullable BlendModeOverride defaultBlendOverride) { // DRAWBUFFERS is only detected in the fragment shader source code (.fsh). // If there's no explicit declaration, then by default /* DRAWBUFFERS:0 */ is inferred. // For SEUS v08 and SEUS v10 to work, this will need to be set to 01234567. However, doing this causes // TAA to break on Sildur's Vibrant Shaders, since gbuffers_skybasic lacks a DRAWBUFFERS directive, causing // undefined data to be written to colortex7. // // TODO: Figure out how to infer the DRAWBUFFERS directive when it is missing. final Optional optionalDrawbuffersDirective = findDrawbuffersDirective(source.getFragmentSource()); final Optional optionalRendertargetsDirective = findRendertargetsDirective(source.getFragmentSource()); final Optional optionalCommentDirective = getAppliedDirective(optionalDrawbuffersDirective, optionalRendertargetsDirective); drawBuffers = optionalCommentDirective.map(commentDirective -> { if (commentDirective.getType() == CommentDirective.Type.DRAWBUFFERS) { return parseDigits(commentDirective.getDirective().toCharArray()); } else if (commentDirective.getType() == CommentDirective.Type.RENDERTARGETS) { return parseDigitList(commentDirective.getDirective()); } else { throw new IllegalStateException("Unhandled comment directive type!"); } }).orElse(new int[] { 0 }); if (properties != null) { viewportScale = properties.getViewportScaleOverrides().getOrDefault(source.getName(), ViewportData.defaultValue()); alphaTestOverride = properties.getAlphaTestOverrides().get(source.getName()); final BlendModeOverride blendModeOverride = properties.getBlendModeOverrides().get(source.getName()); final List bufferBlendInformations = properties.getBufferBlendOverrides().get(source.getName()); this.blendModeOverride = Optional.ofNullable(blendModeOverride != null ? blendModeOverride : defaultBlendOverride); this.bufferBlendInformations = bufferBlendInformations != null ? bufferBlendInformations : Collections.emptyList(); explicitFlips = source.getParent().getPackDirectives().getExplicitFlips(source.getName()); } else { viewportScale = ViewportData.defaultValue(); alphaTestOverride = null; blendModeOverride = Optional.ofNullable(defaultBlendOverride); bufferBlendInformations = Collections.emptyList(); explicitFlips = ImmutableMap.of(); } final HashSet mipmappedBuffers = new HashSet<>(); final DispatchingDirectiveHolder directiveHolder = new DispatchingDirectiveHolder(); supportedRenderTargets.forEach(index -> { final BooleanConsumer mipmapHandler = shouldMipmap -> { if (shouldMipmap) { mipmappedBuffers.add(index); } else { mipmappedBuffers.remove(index); } }; directiveHolder.acceptConstBooleanDirective("colortex" + index + "MipmapEnabled", mipmapHandler); if (index < LEGACY_RENDER_TARGETS.size()) { directiveHolder.acceptConstBooleanDirective(LEGACY_RENDER_TARGETS.get(index) + "MipmapEnabled", mipmapHandler); } }); source.getFragmentSource().map(ConstDirectiveParser::findDirectives).ifPresent(directives -> { for (ConstDirectiveParser.ConstDirective directive : directives) { directiveHolder.processDirective(directive); } }); this.mipmappedBuffers = ImmutableSet.copyOf(mipmappedBuffers); } public ProgramDirectives withOverriddenDrawBuffers(int[] drawBuffersOverride) { return new ProgramDirectives(drawBuffersOverride, viewportScale, alphaTestOverride, blendModeOverride, bufferBlendInformations, mipmappedBuffers, explicitFlips); } private static Optional findDrawbuffersDirective(Optional stageSource) { return stageSource.flatMap(fragment -> CommentDirectiveParser.findDirective(fragment, CommentDirective.Type.DRAWBUFFERS)); } private static Optional findRendertargetsDirective(Optional stageSource) { return stageSource.flatMap(fragment -> CommentDirectiveParser.findDirective(fragment, CommentDirective.Type.RENDERTARGETS)); } private static int[] parseDigits(char[] directiveChars) { int[] buffers = new int[directiveChars.length]; int index = 0; for (char buffer : directiveChars) { buffers[index++] = Character.digit(buffer, 10); } return buffers; } private static int[] parseDigitList(String digitListString) { return Arrays.stream(digitListString.split(",")) .mapToInt(Integer::parseInt) .toArray(); } private static Optional getAppliedDirective(Optional optionalDrawbuffersDirective, Optional optionalRendertargetsDirective) { if (optionalDrawbuffersDirective.isPresent() && optionalRendertargetsDirective.isPresent()) { if (optionalDrawbuffersDirective.get().getLocation() > optionalRendertargetsDirective.get().getLocation()) { return optionalDrawbuffersDirective; } else { return optionalRendertargetsDirective; } } else if (optionalDrawbuffersDirective.isPresent()) { return optionalDrawbuffersDirective; } else if (optionalRendertargetsDirective.isPresent()) { return optionalRendertargetsDirective; } else { return Optional.empty(); } } public int[] getDrawBuffers() { return drawBuffers; } public ViewportData getViewportScale() { return viewportScale; } public Optional getAlphaTestOverride() { return Optional.ofNullable(alphaTestOverride); } public Optional getBlendModeOverride() { return blendModeOverride; } public List getBufferBlendOverrides() { return bufferBlendInformations; } public ImmutableSet getMipmappedBuffers() { return mipmappedBuffers; } public ImmutableMap getExplicitFlips() { return explicitFlips; } }