Files
Angelica/src/main/java/net/coderbot/iris/gl/program/ProgramBuilder.java
T
Jason Mitchell 6691511e17 More Caching (#242)
* More Caching
* Pull TextureTracker & TextureInfoCache out of Iris and into GLSM
* Cache more TextureInfo state
* GLSM: glTexParameter, glGetTexParameter(i|f), glGetTexLevelParameteri
* Move IrisRenderSystem -> GLSM.RenderSystem
* Ensure DSA works properly with GLSM
 * Further updates to DSA for texParameter/getTexParameter
* A few more cached fields for TextureInfo
* Properly handle deleteTextures
* Ensure anyone crazy enough to use an Int/FloatBuffer for single value params get redirected
* Add optional runtime disabling of GLSM
* Switch checkGLError from an inject to a redirect, hopefully fewer allocations
* Fewer allocations for clearBufferState
* Microoptimization for (setup|clear)BufferState
2024-01-27 15:52:57 -08:00

132 lines
4.1 KiB
Java

package net.coderbot.iris.gl.program;
import com.google.common.collect.ImmutableSet;
import com.gtnewhorizons.angelica.glsm.RenderSystem;
import net.coderbot.iris.gl.image.ImageHolder;
import net.coderbot.iris.gl.sampler.SamplerHolder;
import net.coderbot.iris.gl.shader.GlShader;
import net.coderbot.iris.gl.shader.ProgramCreator;
import net.coderbot.iris.gl.shader.ShaderType;
import net.coderbot.iris.gl.state.ValueUpdateNotifier;
import net.coderbot.iris.gl.texture.InternalTextureFormat;
import org.jetbrains.annotations.Nullable;
import java.util.function.IntSupplier;
public class ProgramBuilder extends ProgramUniforms.Builder implements SamplerHolder, ImageHolder {
private final int program;
private final ProgramSamplers.Builder samplers;
private final ProgramImages.Builder images;
private ProgramBuilder(String name, int program, ImmutableSet<Integer> reservedTextureUnits) {
super(name, program);
this.program = program;
this.samplers = ProgramSamplers.builder(program, reservedTextureUnits);
this.images = ProgramImages.builder(program);
}
public void bindAttributeLocation(int index, String name) {
RenderSystem.bindAttributeLocation(program, index, name);
}
public static ProgramBuilder begin(String name, @Nullable String vertexSource, @Nullable String geometrySource,
@Nullable String fragmentSource, ImmutableSet<Integer> reservedTextureUnits) {
GlShader vertex;
GlShader geometry;
GlShader fragment;
vertex = buildShader(ShaderType.VERTEX, name + ".vsh", vertexSource);
if (geometrySource != null) {
geometry = buildShader(ShaderType.GEOMETRY, name + ".gsh", geometrySource);
} else {
geometry = null;
}
fragment = buildShader(ShaderType.FRAGMENT, name + ".fsh", fragmentSource);
int programId;
if (geometry != null) {
programId = ProgramCreator.create(name, vertex, geometry, fragment);
} else {
programId = ProgramCreator.create(name, vertex, fragment);
}
vertex.destroy();
if (geometry != null) {
geometry.destroy();
}
fragment.destroy();
return new ProgramBuilder(name, programId, reservedTextureUnits);
}
public static ProgramBuilder beginCompute(String name, @Nullable String source, ImmutableSet<Integer> reservedTextureUnits) {
if (!RenderSystem.supportsCompute()) {
throw new IllegalStateException("This PC does not support compute shaders, but it's attempting to be used???");
}
GlShader compute = buildShader(ShaderType.COMPUTE, name + ".csh", source);
int programId = ProgramCreator.create(name, compute);
compute.destroy();
return new ProgramBuilder(name, programId, reservedTextureUnits);
}
public Program build() {
return new Program(program, super.buildUniforms(), this.samplers.build(), this.images.build());
}
public ComputeProgram buildCompute() {
return new ComputeProgram(program, super.buildUniforms(), this.samplers.build(), this.images.build());
}
private static GlShader buildShader(ShaderType shaderType, String name, @Nullable String source) {
try {
return new GlShader(shaderType, name, source);
} catch (RuntimeException e) {
throw new RuntimeException("Failed to compile " + shaderType + " shader for program " + name, e);
}
}
@Override
public void addExternalSampler(int textureUnit, String... names) {
samplers.addExternalSampler(textureUnit, names);
}
@Override
public boolean hasSampler(String name) {
return samplers.hasSampler(name);
}
@Override
public boolean addDefaultSampler(IntSupplier sampler, String... names) {
return samplers.addDefaultSampler(sampler, names);
}
@Override
public boolean addDynamicSampler(IntSupplier sampler, String... names) {
return samplers.addDynamicSampler(sampler, names);
}
public boolean addDynamicSampler(IntSupplier sampler, ValueUpdateNotifier notifier, String... names) {
return samplers.addDynamicSampler(sampler, notifier, names);
}
@Override
public boolean hasImage(String name) {
return images.hasImage(name);
}
@Override
public void addTextureImage(IntSupplier textureID, InternalTextureFormat internalFormat, String name) {
images.addTextureImage(textureID, internalFormat, name);
}
}