| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.jtracy.TracyClient;
|
| 4 | import com.mojang.jtracy.Zone;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.renderpearl.api.pipeline.ShaderType;
|
| 7 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 8 | import com.mojang.renderpearl.backend.api.SpvModule;
|
| 9 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline.CreateInfo.Shader;
|
| 10 | import com.mojang.renderpearl.frontend.shaders.SpvUtil;
|
| 11 | import com.mojang.renderpearl.util.ShaderCompileException;
|
| 12 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 13 | import it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap;
|
| 14 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 15 | import java.nio.IntBuffer;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Locale;
|
| 18 | import java.util.Map;
|
| 19 | import net.fabricmc.api.EnvType;
|
| 20 | import net.fabricmc.api.Environment;
|
| 21 | import net.minecraft.SharedConstants;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.util.Util.OS;
|
| 24 | import org.apache.commons.lang3.StringUtils;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.lwjgl.PointerBuffer;
|
| 27 | import org.lwjgl.system.MemoryStack;
|
| 28 | import org.lwjgl.system.MemoryUtil;
|
| 29 | import org.lwjgl.util.spvc.Spvc;
|
| 30 | import org.lwjgl.util.spvc.SpvcReflectedResource;
|
| 31 | import org.lwjgl.util.spvc.SpvcReflectedResource.Buffer;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | @Environment(EnvType.CLIENT)
|
| 35 | public class GlPipelineRecompiler {
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | public static final int MAX_LOG_LENGTH = 32768;
|
| 38 | private static final boolean IS_MACOS = Util.getPlatform() == OS.OSX;
|
| 39 | public static final String UNIFORM_FORMAT_STRING = "_uniform_%02d_%02d";
|
| 40 | public static final String PUSH_CONSTANT_BLOCK_NAME = "_push_constants";
|
| 41 | private static final boolean SHADER_DEBUG_MODE = SharedConstants.IS_RENDERDOC_ATTACHED;
|
| 42 | private final GlDebugLabel debugLabels;
|
| 43 | private final boolean drawParametersSupported;
|
| 44 |
|
| 45 | public GlPipelineRecompiler(final GlDebugLabel debugLabels, final boolean drawParametersSupported) {
|
| 46 | this.debugLabels = debugLabels;
|
| 47 | this.drawParametersSupported = drawParametersSupported;
|
| 48 | }
|
| 49 |
|
| 50 | private String decompileShader(final Shader shaderCreateInfo) throws ShaderCompileException {
|
| 51 | SpvModule spvModule = shaderCreateInfo.module();
|
| 52 | IntBuffer spirv = spvModule.spv().asIntBuffer();
|
| 53 | long spvcContext = 0L;
|
| 54 |
|
| 55 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 56 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 57 | SpvUtil.throwIfError(Spvc.spvc_context_create(pointerReturnBuffer), "Couldn't create spvc context");
|
| 58 | spvcContext = pointerReturnBuffer.get(0);
|
| 59 | SpvUtil.throwIfError(Spvc.spvc_context_parse_spirv(spvcContext, spirv, spirv.remaining(), pointerReturnBuffer), "Couldn't parse spirv");
|
| 60 | long ir = pointerReturnBuffer.get(0);
|
| 61 | SpvUtil.throwIfError(Spvc.spvc_context_create_compiler(spvcContext, 1, ir, 0, pointerReturnBuffer), "Couldn't create compiler");
|
| 62 | long compiler = pointerReturnBuffer.get(0);
|
| 63 | Spvc.spvc_compiler_create_compiler_options(compiler, pointerReturnBuffer);
|
| 64 | long options = pointerReturnBuffer.get(0);
|
| 65 | Spvc.spvc_compiler_options_set_uint(options, 33554440, 330);
|
| 66 | Spvc.spvc_compiler_options_set_bool(options, 33554439, false);
|
| 67 | Spvc.spvc_compiler_options_set_bool(options, 33554465, true);
|
| 68 | Spvc.spvc_compiler_options_set_bool(options, 33554437, this.drawParametersSupported);
|
| 69 | if (!SHADER_DEBUG_MODE) {
|
| 70 | Spvc.spvc_compiler_options_set_bool(options, 16777270, true);
|
| 71 | Spvc.spvc_compiler_options_set_bool(options, 16777218, true);
|
| 72 | }
|
| 73 |
|
| 74 | SpvUtil.throwIfError(Spvc.spvc_compiler_create_shader_resources(compiler, pointerReturnBuffer), "Couldn't create resource list");
|
| 75 | long spvcResources = pointerReturnBuffer.get(0);
|
| 76 | if (!SHADER_DEBUG_MODE) {
|
| 77 | if (spvModule.type() == ShaderType.VERTEX) {
|
| 78 | this.renameInterfaceVariables(compiler, spvcResources, 3, "_vert_input_%02d");
|
| 79 | }
|
| 80 |
|
| 81 | if (spvModule.type() == ShaderType.FRAGMENT) {
|
| 82 | this.renameInterfaceVariables(compiler, spvcResources, 4, "_frag_output_%02d");
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | if (SHADER_DEBUG_MODE && !IS_MACOS) {
|
| 87 | Spvc.spvc_compiler_options_set_bool(options, 33554438, true);
|
| 88 | } else if (spvModule.type() == ShaderType.VERTEX || spvModule.type() == ShaderType.FRAGMENT) {
|
| 89 | this.renameInterfaceVariables(compiler, spvcResources, spvModule.type() == ShaderType.FRAGMENT ? 3 : 4, "_interface_variable_%02d");
|
| 90 | }
|
| 91 |
|
| 92 | for (int i = 0; i < SpvUtil.DESCRIPTOR_TYPES.size(); i++) {
|
| 93 | int descriptorType = SpvUtil.DESCRIPTOR_TYPES.getInt(i);
|
| 94 | this.renameDescriptors(compiler, spvcResources, descriptorType);
|
| 95 | }
|
| 96 |
|
| 97 | this.renameDescriptors(compiler, spvcResources, 9);
|
| 98 |
|
| 99 | Spvc.spvc_compiler_set_entry_point(compiler, shaderCreateInfo.entryPoint(), switch (spvModule.type()) {
|
| 100 | case VERTEX -> 0;
|
| 101 | case FRAGMENT -> 4;
|
| 102 | });
|
| 103 | Spvc.spvc_compiler_install_compiler_options(compiler, options);
|
| 104 | Spvc.spvc_compiler_compile(compiler, pointerReturnBuffer);
|
| 105 | return MemoryUtil.memASCII(pointerReturnBuffer.get(0));
|
| 106 | } finally {
|
| 107 | if (spvcContext != 0L) {
|
| 108 | Spvc.spvc_context_destroy(spvcContext);
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private void renameInterfaceVariables(final long compiler, final long spvcResources, final int resourceType, final String formatString) throws ShaderCompileException {
|
| 114 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 115 | PointerBuffer countPointer = stack.callocPointer(1);
|
| 116 | IntBuffer intReturnBuffer = stack.callocInt(1);
|
| 117 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 118 | SpvUtil.throwIfError(
|
| 119 | Spvc.spvc_resources_get_resource_list_for_type(spvcResources, resourceType, pointerReturnBuffer, countPointer), "Couldn't get resource list"
|
| 120 | );
|
| 121 | long interfaceVariableCount = countPointer.get(0);
|
| 122 | Buffer interfaceVariables = SpvcReflectedResource.create(pointerReturnBuffer.get(0), (int)interfaceVariableCount);
|
| 123 |
|
| 124 | for (int i = 0; i < interfaceVariableCount; i++) {
|
| 125 | SpvcReflectedResource interfaceVariable = interfaceVariables.get(i);
|
| 126 | int location = Spvc.spvc_compiler_get_decoration(compiler, interfaceVariable.id(), 30);
|
| 127 | Spvc.spvc_compiler_set_name(compiler, interfaceVariable.id(), String.format(Locale.ROOT, formatString, location));
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | private void renameDescriptors(final long compiler, final long spvcResources, final int resourceType) throws ShaderCompileException {
|
| 133 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 134 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 135 | IntBuffer intReturnBuffer = stack.callocInt(1);
|
| 136 | PointerBuffer countPointer = stack.callocPointer(1);
|
| 137 | SpvUtil.throwIfError(
|
| 138 | Spvc.spvc_resources_get_resource_list_for_type(spvcResources, resourceType, pointerReturnBuffer, countPointer), "Couldn't list input variables"
|
| 139 | );
|
| 140 | long resourceCount = countPointer.get(0);
|
| 141 | Buffer resources = SpvcReflectedResource.create(pointerReturnBuffer.get(0), (int)resourceCount);
|
| 142 |
|
| 143 | for (int i = 0; i < resourceCount; i++) {
|
| 144 | SpvcReflectedResource resource = resources.get(i);
|
| 145 | int descriptorSet = Spvc.spvc_compiler_get_decoration(compiler, resource.id(), 34);
|
| 146 | int binding = Spvc.spvc_compiler_get_decoration(compiler, resource.id(), 33);
|
| 147 | switch (resourceType) {
|
| 148 | case 1:
|
| 149 | case 2:
|
| 150 | String blockName = String.format(Locale.ROOT, "_uniform_instance_%02d_%02d", descriptorSet, binding);
|
| 151 | if (SHADER_DEBUG_MODE) {
|
| 152 | String existingBlockName = Spvc.spvc_compiler_get_name(compiler, resource.base_type_id());
|
| 153 | if (existingBlockName != null) {
|
| 154 | blockName = existingBlockName;
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | Spvc.spvc_compiler_set_name(compiler, resource.id(), blockName);
|
| 159 | Spvc.spvc_compiler_set_name(compiler, resource.base_type_id(), String.format(Locale.ROOT, "_uniform_%02d_%02d", descriptorSet, binding));
|
| 160 | case 3:
|
| 161 | case 4:
|
| 162 | case 5:
|
| 163 | case 8:
|
| 164 | default:
|
| 165 | break;
|
| 166 | case 6:
|
| 167 | case 7:
|
| 168 | case 10:
|
| 169 | case 11:
|
| 170 | Spvc.spvc_compiler_set_name(compiler, resource.id(), String.format(Locale.ROOT, "_uniform_%02d_%02d", descriptorSet, binding));
|
| 171 | break;
|
| 172 | case 9:
|
| 173 | String blockName = "_push_constants_instance";
|
| 174 | if (SHADER_DEBUG_MODE) {
|
| 175 | String existingBlockName = Spvc.spvc_compiler_get_name(compiler, resource.base_type_id());
|
| 176 | if (existingBlockName != null) {
|
| 177 | blockName = existingBlockName;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | Spvc.spvc_compiler_set_name(compiler, resource.id(), blockName);
|
| 182 | Spvc.spvc_compiler_set_name(compiler, resource.base_type_id(), "_push_constants");
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | private GlShaderModule compileShader(final String name, final ShaderType type, final String source) {
|
| 189 | int shaderId = GlStateManager.glCreateShader(GlConst.toGl(type));
|
| 190 | GlStateManager.glShaderSource(shaderId, source);
|
| 191 | GlStateManager.glCompileShader(shaderId);
|
| 192 | if (GlStateManager.glGetShaderi(shaderId, 35713) == 0) {
|
| 193 | String logInfo = StringUtils.trim(GlStateManager.glGetShaderInfoLog(shaderId, 32768));
|
| 194 | LOGGER.error("Couldn't compile {} shader for pipeline ({}): {}", type.getName(), name, logInfo);
|
| 195 | return GlShaderModule.INVALID_SHADER;
|
| 196 | } else {
|
| 197 | GlShaderModule module = new GlShaderModule(shaderId, name, type);
|
| 198 | this.debugLabels.applyLabel(module);
|
| 199 | return module;
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | public @Nullable Map<Shader, String> decompileShaders(final BackendRenderPipeline.CreateInfo createInfo) {
|
| 204 | try (Zone tracyZone = TracyClient.beginZone("Decompile shaders", false)) {
|
| 205 | tracyZone.addText(createInfo.name());
|
| 206 | Map<Shader, String> decompiledShaders = new Reference2ReferenceArrayMap<>();
|
| 207 |
|
| 208 | for (Shader shader : createInfo.shaders()) {
|
| 209 | decompiledShaders.put(shader, this.decompileShader(shader));
|
| 210 | }
|
| 211 |
|
| 212 | return decompiledShaders;
|
| 213 | } catch (ShaderCompileException e) {
|
| 214 | LOGGER.error("Couldn't compile program for pipeline {}", createInfo.name(), e);
|
| 215 | return null;
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | public @Nullable GlProgram compileProgram(final BackendRenderPipeline.CreateInfo createInfo, final Map<Shader, String> decompiledShaders) {
|
| 220 | List<GlShaderModule> compiledShaders = new ReferenceArrayList<>();
|
| 221 |
|
| 222 | try (Zone tracyZone = TracyClient.beginZone("Build GlProgram", false)) {
|
| 223 | tracyZone.addText(createInfo.name());
|
| 224 |
|
| 225 | for (Shader shader : createInfo.shaders()) {
|
| 226 | String decompiledSource = decompiledShaders.get(shader);
|
| 227 | GlShaderModule compiledShader = this.compileShader(shader.name(), shader.module().type(), decompiledSource);
|
| 228 | if (compiledShader == GlShaderModule.INVALID_SHADER) {
|
| 229 | return null;
|
| 230 | }
|
| 231 |
|
| 232 | compiledShaders.add(compiledShader);
|
| 233 | }
|
| 234 |
|
| 235 | GlProgram compiled = GlProgram.link(compiledShaders, createInfo.name());
|
| 236 | compiled.setupBindGroupLayouts(createInfo.uniforms());
|
| 237 | this.debugLabels.applyLabel(compiled);
|
| 238 | return compiled;
|
| 239 | } catch (IllegalArgumentException | ShaderCompileException e) {
|
| 240 | LOGGER.error("Couldn't compile program for pipeline {}", createInfo.name(), e);
|
| 241 | return null;
|
| 242 | } finally {
|
| 243 | compiledShaders.forEach(UncheckedAutoCloseable::close);
|
| 244 | }
|
| 245 | }
|
| 246 | }
|
| 247 |
|