| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.pipeline.BindGroupLayout;
|
| 6 | import com.mojang.renderpearl.util.ShaderCompileException;
|
| 7 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 8 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.lwjgl.opengl.GL33C;
|
| 17 | import org.slf4j.Logger;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class GlProgram implements UncheckedAutoCloseable {
|
| 21 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 22 | private final List<@Nullable Uniform> uniforms = new ReferenceArrayList<>();
|
| 23 | private Uniform.@Nullable Ubo pushConstant = null;
|
| 24 | private final int programId;
|
| 25 | private final String debugLabel;
|
| 26 |
|
| 27 | private GlProgram(final int programId, final String debugLabel) {
|
| 28 | this.programId = programId;
|
| 29 | this.debugLabel = debugLabel;
|
| 30 | }
|
| 31 |
|
| 32 | public static GlProgram link(final List<GlShaderModule> compiledShaders, final String debugLabel) throws ShaderCompileException {
|
| 33 | int programId = GlStateManager.glCreateProgram();
|
| 34 | if (programId <= 0) {
|
| 35 | throw new ShaderCompileException("Could not create shader program (returned program ID " + programId + ")");
|
| 36 | }
|
| 37 |
|
| 38 | for (GlShaderModule shaderModule : compiledShaders) {
|
| 39 | GlStateManager.glAttachShader(programId, shaderModule.getShaderId());
|
| 40 | }
|
| 41 |
|
| 42 | GlStateManager.glLinkProgram(programId);
|
| 43 | int linkStatus = GlStateManager.glGetProgrami(programId, 35714);
|
| 44 | String linkMessage = GlStateManager.glGetProgramInfoLog(programId, 32768);
|
| 45 | if (linkStatus != 0 && !linkMessage.contains("Failed for unknown reason")) {
|
| 46 | if (!linkMessage.isEmpty()) {
|
| 47 | LOGGER.info("Info log when linking program containing {}. Log output: {}", shaderList(compiledShaders), linkMessage);
|
| 48 | }
|
| 49 |
|
| 50 | return new GlProgram(programId, debugLabel);
|
| 51 | } else {
|
| 52 | throw new ShaderCompileException("Error encountered when linking program containing " + shaderList(compiledShaders) + ". Log output: " + linkMessage);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private static String shaderList(final List<GlShaderModule> compiledShaders) {
|
| 57 | return compiledShaders.stream().map(shader -> shader.getType() + " " + shader.getLabel()).collect(Collectors.joining(", "));
|
| 58 | }
|
| 59 |
|
| 60 | public void setupBindGroupLayouts(final List<BindGroupLayout.UniformDescription> uniforms) {
|
| 61 | int nextUboBinding = 0;
|
| 62 | int nextSamplerIndex = 0;
|
| 63 | GlStateManager._glUseProgram(this.programId);
|
| 64 | int pushConstantBlock = GL33C.glGetUniformBlockIndex(this.programId, "_push_constants");
|
| 65 | if (pushConstantBlock != -1) {
|
| 66 | int uboBinding = nextUboBinding++;
|
| 67 | GL33C.glUniformBlockBinding(this.programId, pushConstantBlock, uboBinding);
|
| 68 | this.pushConstant = new Uniform.Ubo(uboBinding);
|
| 69 | }
|
| 70 |
|
| 71 | for (int i = 0; i < uniforms.size(); i++) {
|
| 72 | BindGroupLayout.UniformDescription uniformDescription = uniforms.get(i);
|
| 73 | String uniformName = String.format(Locale.ROOT, "_uniform_%02d_%02d", 0, i);
|
| 74 |
|
| 75 | Uniform uniform = switch (uniformDescription.type()) {
|
| 76 | case UNIFORM_BUFFER -> {
|
| 77 | int index = GL33C.glGetUniformBlockIndex(this.programId, uniformName);
|
| 78 | if (index == -1) {
|
| 79 | yield null;
|
| 80 | } else {
|
| 81 | int uboBinding = nextUboBinding++;
|
| 82 | GL33C.glUniformBlockBinding(this.programId, index, uboBinding);
|
| 83 | yield new Uniform.Ubo(uboBinding);
|
| 84 | }
|
| 85 | }
|
| 86 | case TEXEL_BUFFER -> {
|
| 87 | int location = GlStateManager._glGetUniformLocation(this.programId, uniformName);
|
| 88 | if (location == -1) {
|
| 89 | yield null;
|
| 90 | } else {
|
| 91 | int samplerIndex = nextSamplerIndex++;
|
| 92 | GL33C.glUniform1i(location, samplerIndex);
|
| 93 | yield new Uniform.Utb(samplerIndex, Objects.requireNonNull(uniformDescription.gpuFormat()));
|
| 94 | }
|
| 95 | }
|
| 96 | case COMBINED_IMAGE_SAMPLER -> {
|
| 97 | int location = GlStateManager._glGetUniformLocation(this.programId, uniformName);
|
| 98 | if (location == -1) {
|
| 99 | yield null;
|
| 100 | } else {
|
| 101 | int samplerIndex = nextSamplerIndex++;
|
| 102 | GL33C.glUniform1i(location, samplerIndex);
|
| 103 | yield new Uniform.Sampler(samplerIndex);
|
| 104 | }
|
| 105 | }
|
| 106 | };
|
| 107 | this.uniforms.add(uniform);
|
| 108 | }
|
| 109 |
|
| 110 | GlStateManager._glUseProgram(0);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public void close() {
|
| 115 | this.uniforms.forEach(UncheckedAutoCloseable::safeClose);
|
| 116 | GlStateManager.glDeleteProgram(this.programId);
|
| 117 | }
|
| 118 |
|
| 119 | public @Nullable Uniform getUniform(final int index) {
|
| 120 | return index >= this.uniforms.size() ? null : this.uniforms.get(index);
|
| 121 | }
|
| 122 |
|
| 123 | public int uniformCount() {
|
| 124 | return this.uniforms.size();
|
| 125 | }
|
| 126 |
|
| 127 | @VisibleForTesting
|
| 128 | public int getProgramId() {
|
| 129 | return this.programId;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public String toString() {
|
| 134 | return this.debugLabel;
|
| 135 | }
|
| 136 |
|
| 137 | public String getDebugLabel() {
|
| 138 | return this.debugLabel;
|
| 139 | }
|
| 140 |
|
| 141 | public Uniform.@Nullable Ubo pushConstant() {
|
| 142 | return this.pushConstant;
|
| 143 | }
|
| 144 | }
|
| 145 |
|