GlslCompiler .java

com.mojang.renderpearl.frontend.shaders.GlslCompiler (class): members: includeResultRelease, missingIncludeResult, malformedIdResult, compileToSpv, close.

com.mojang.renderpearl.frontend.shadersclass148 linesclient6.1 KB

Implements

API Surface (2)

void close()
SpvModule compileToSpv(final String name, final String source, final ShaderType type, final ShaderDefines shaderDefines, final ShaderSource shaderSource)

Fields & Constants (7)

isZeroToOne
shaderDrawParameters
includeResultRelease
missingIncludeResult
malformedIdResult
compilers
shaderDefines

String literals (7)

"not found"
"malformed id"
"RENDERPEARL_DEPTH_IS_ZERO_TO_ONE"
"RENDERPEARL_EXPLICIT_DEPTH_INVARIANCE"
"main"
"Compile to SPV"
"Couldn't parse GLSL: "

Full Source

GlslCompiler.java148 lines · Minecraft 26.3 · verbatim
1
package com.mojang.renderpearl.frontend.shaders;
2
3
import com.mojang.blaze3d.systems.RenderSystem;
4
import com.mojang.jtracy.TracyClient;
5
import com.mojang.jtracy.Zone;
6
import com.mojang.renderpearl.api.pipeline.ShaderSource;
7
import com.mojang.renderpearl.api.pipeline.ShaderType;
8
import com.mojang.renderpearl.backend.api.SpvModule;
9
import com.mojang.renderpearl.util.ShaderCompileException;
10
import com.mojang.renderpearl.util.UncheckedAutoCloseable;
11
import it.unimi.dsi.fastutil.longs.LongArrayList;
12
import java.nio.ByteBuffer;
13
import java.util.Map.Entry;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.client.renderer.ShaderDefines;
17
import net.minecraft.resources.Identifier;
18
import org.lwjgl.system.MemoryUtil;
19
import org.lwjgl.util.shaderc.Shaderc;
20
import org.lwjgl.util.shaderc.ShadercIncludeResolve;
21
import org.lwjgl.util.shaderc.ShadercIncludeResultRelease;
22
23
@Environment(EnvType.CLIENT)
24
public class GlslCompiler implements UncheckedAutoCloseable {
25
	private final boolean isZeroToOne;
26
	private final boolean shaderDrawParameters;
27
	private final ShadercIncludeResultRelease includeResultRelease;
28
	private final ShaderSource.CachedIncludeSource missingIncludeResult;
29
	private final ShaderSource.CachedIncludeSource malformedIdResult;
30
	private final LongArrayList compilers = new LongArrayList();
31
32
	public GlslCompiler(final boolean isZeroToOne, final boolean shaderDrawParameters) {
33
		this.isZeroToOne = isZeroToOne;
34
		this.shaderDrawParameters = shaderDrawParameters;
35
		this.includeResultRelease = ShadercIncludeResultRelease.create(GlslCompiler::releaseIncludeResult);
36
		this.missingIncludeResult = ShaderSource.CachedIncludeSource.createError("not found");
37
		this.malformedIdResult = ShaderSource.CachedIncludeSource.createError("malformed id");
38
	}
39
40
	private ShaderSource.CachedIncludeSource processInclude(final ShaderSource shaderSource, final String requestedShader) {
41
		Identifier id = Identifier.tryParse(requestedShader);
42
		if (id == null) {
43
			return this.malformedIdResult;
44
		}
45
46
		ShaderSource.CachedIncludeSource shaderContents = shaderSource.getInclude(id);
47
		return shaderContents == null ? this.missingIncludeResult : shaderContents;
48
	}
49
50
	private ShadercIncludeResolve createIncludeResolver(final ShaderSource shaderSource) {
51
		return ShadercIncludeResolve.create((var2, requested_source, var6, var7, var9) -> {
52
			String requestedShader = MemoryUtil.memASCII(requested_source);
53
			return this.processInclude(shaderSource, requestedShader).includeResultPtr();
54
		});
55
	}
56
57
	private static void releaseIncludeResult(final long user_data, final long include_result) {
58
	}
59
60
	@Override
61
	public void close() {
62
		this.includeResultRelease.close();
63
		this.malformedIdResult.close();
64
		this.missingIncludeResult.close();
65
		this.compilers.forEach(Shaderc::shaderc_compiler_release);
66
		this.compilers.clear();
67
	}
68
69
	private synchronized long acquireCompiler() {
70
		return this.compilers.isEmpty() ? Shaderc.shaderc_compiler_initialize() : this.compilers.popLong();
71
	}
72
73
	private synchronized void releaseCompiler(final long compiler) {
74
		this.compilers.add(compiler);
75
	}
76
77
	private long createBaseShaderOptions() {
78
		long shaderOptions = Shaderc.shaderc_compile_options_initialize();
79
		Shaderc.shaderc_compile_options_set_target_env(shaderOptions, 0, 4202496);
80
		Shaderc.shaderc_compile_options_set_auto_bind_uniforms(shaderOptions, true);
81
		Shaderc.shaderc_compile_options_set_preserve_bindings(shaderOptions, false);
82
		Shaderc.shaderc_compile_options_set_generate_debug_info(shaderOptions);
83
		Shaderc.shaderc_compile_options_set_optimization_level(shaderOptions, 0);
84
		if (this.isZeroToOne) {
85
			Shaderc.shaderc_compile_options_add_macro_definition(shaderOptions, "RENDERPEARL_DEPTH_IS_ZERO_TO_ONE", "");
86
		}
87
88
		if (RenderSystem.getDevice().getDeviceInfo().hintsAndWorkarounds().isExplicitDepthRequired()) {
89
			Shaderc.shaderc_compile_options_add_macro_definition(shaderOptions, "RENDERPEARL_EXPLICIT_DEPTH_INVARIANCE", "");
90
		}
91
92
		if (this.shaderDrawParameters) {
93
			Shaderc.shaderc_compile_options_add_macro_definition(shaderOptions, "RENDERPEARL_INSTANCE_INDEX_INCLUDES_BASE_INSTANCE", "");
94
		}
95
96
		return shaderOptions;
97
	}
98
99
	public SpvModule compileToSpv(
100
		final String name, final String source, final ShaderType type, final ShaderDefines shaderDefines, final ShaderSource shaderSource
101
	) throws ShaderCompileException {
102
		int shaderType = type == ShaderType.FRAGMENT ? 1 : 0;
103
		ByteBuffer sourceBuffer = MemoryUtil.memUTF8(source, false);
104
		ByteBuffer filenameBuffer = MemoryUtil.memUTF8(name);
105
		ByteBuffer entrypointBuffer = MemoryUtil.memUTF8("main");
106
		long shaderOptions = this.createBaseShaderOptions();
107
108
		for (Entry<String, String> macro : shaderDefines.values().entrySet()) {
109
			Shaderc.shaderc_compile_options_add_macro_definition(shaderOptions, macro.getKey(), macro.getValue());
110
		}
111
112
		for (String flag : shaderDefines.flags()) {
113
			Shaderc.shaderc_compile_options_add_macro_definition(shaderOptions, flag, "");
114
		}
115
116
		try (ShadercIncludeResolve includeResolver = this.createIncludeResolver(shaderSource)) {
117
			Shaderc.shaderc_compile_options_set_include_callbacks(shaderOptions, includeResolver, this.includeResultRelease, 0L);
118
			long compiler = this.acquireCompiler();
119
120
			long result;
121
			try (Zone tracyZone = TracyClient.beginZone("Compile to SPV", false)) {
122
				tracyZone.addText(name);
123
				result = Shaderc.shaderc_compile_into_spv(compiler, sourceBuffer, shaderType, filenameBuffer, entrypointBuffer, shaderOptions);
124
			} finally {
125
				this.releaseCompiler(compiler);
126
			}
127
128
			try {
129
				int status = Shaderc.shaderc_result_get_compilation_status(result);
130
				if (status != 0) {
131
					throw new ShaderCompileException("Couldn't parse GLSL: " + Shaderc.shaderc_result_get_error_message(result));
132
				}
133
134
				ByteBuffer spirv = Shaderc.shaderc_result_get_bytes(result);
135
				ByteBuffer copy = MemoryUtil.memCalloc(spirv.remaining());
136
				MemoryUtil.memCopy(spirv, copy);
137
				return new SPIRVModule(copy, type);
138
			} finally {
139
				Shaderc.shaderc_result_release(result);
140
				Shaderc.shaderc_compile_options_release(shaderOptions);
141
				MemoryUtil.memFree(entrypointBuffer);
142
				MemoryUtil.memFree(filenameBuffer);
143
				MemoryUtil.memFree(sourceBuffer);
144
			}
145
		}
146
	}
147
}
148

Decompiled Minecraft 26.3 source of GlslCompiler (com/mojang/renderpearl/frontend/shaders/GlslCompiler.java) with Mojang official mappings, stored verbatim on the site.