| 1 | package com.mojang.renderpearl.frontend.shaders;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.pipeline.ShaderType;
|
| 4 | import com.mojang.renderpearl.backend.api.SpvModule;
|
| 5 | import com.mojang.renderpearl.backend.api.SpvModule.Reflection.Descriptor;
|
| 6 | import com.mojang.renderpearl.backend.api.SpvModule.Reflection.InterfaceVariable;
|
| 7 | import com.mojang.renderpearl.backend.api.SpvModule.Reflection.PushConstant;
|
| 8 | import com.mojang.renderpearl.backend.api.SpvModule.Reflection.Type;
|
| 9 | import com.mojang.renderpearl.util.ShaderCompileException;
|
| 10 | import it.unimi.dsi.fastutil.ints.Int2ReferenceArrayMap;
|
| 11 | import it.unimi.dsi.fastutil.ints.Int2ReferenceMap;
|
| 12 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 13 | import it.unimi.dsi.fastutil.objects.ReferenceLists;
|
| 14 | import java.nio.ByteBuffer;
|
| 15 | import java.nio.IntBuffer;
|
| 16 | import java.util.Collection;
|
| 17 | import java.util.List;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.lwjgl.PointerBuffer;
|
| 22 | import org.lwjgl.system.MemoryStack;
|
| 23 | import org.lwjgl.system.MemoryUtil;
|
| 24 | import org.lwjgl.util.spvc.Spvc;
|
| 25 | import org.lwjgl.util.spvc.SpvcReflectedResource;
|
| 26 | import org.lwjgl.util.spvc.SpvcReflectedResource.Buffer;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class SPIRVModule implements SpvModule {
|
| 30 | private final ByteBuffer spv;
|
| 31 | private final ShaderType type;
|
| 32 | private SpvModule.@Nullable Reflection reflection;
|
| 33 | private long spvcContext;
|
| 34 |
|
| 35 | public SPIRVModule(final ByteBuffer spv, final ShaderType type) {
|
| 36 | this.spv = spv;
|
| 37 | this.type = type;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void close() {
|
| 42 | MemoryUtil.memFree(this.spv);
|
| 43 | if (this.spvcContext != 0L) {
|
| 44 | Spvc.spvc_context_destroy(this.spvcContext);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public ByteBuffer spv() {
|
| 50 | return this.spv;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public ShaderType type() {
|
| 55 | return this.type;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public SpvModule.Reflection reflect() throws ShaderCompileException {
|
| 60 | if (this.reflection == null) {
|
| 61 | this.doReflection();
|
| 62 | }
|
| 63 |
|
| 64 | return this.reflection;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public SpvModule.@Nullable Reflection getReflectionInfoIfAvailable() {
|
| 69 | return this.reflection;
|
| 70 | }
|
| 71 |
|
| 72 | private void doReflection() throws ShaderCompileException {
|
| 73 | IntBuffer spirvBuffer = this.spv.asIntBuffer();
|
| 74 |
|
| 75 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 76 | PointerBuffer pointer = stack.callocPointer(1);
|
| 77 | SpvUtil.throwIfError(Spvc.spvc_context_create(pointer), "Couldn't create spvc context");
|
| 78 | this.spvcContext = pointer.get(0);
|
| 79 | }
|
| 80 |
|
| 81 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 82 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 83 | SpvUtil.throwIfError(Spvc.spvc_context_parse_spirv(this.spvcContext, spirvBuffer, spirvBuffer.remaining(), pointerReturnBuffer), "Couldn't parse spirv");
|
| 84 | long ir = pointerReturnBuffer.get(0);
|
| 85 | SpvUtil.throwIfError(Spvc.spvc_context_create_compiler(this.spvcContext, 0, ir, 0, pointerReturnBuffer), "Couldn't create compiler");
|
| 86 | long compiler = pointerReturnBuffer.get(0);
|
| 87 | SpvUtil.throwIfError(Spvc.spvc_compiler_create_shader_resources(compiler, pointerReturnBuffer), "Couldn't create resource list");
|
| 88 | long spvcResources = pointerReturnBuffer.get(0);
|
| 89 | final List<InterfaceVariable> inputs = generateInterfaceVariableList(compiler, spvcResources, spirvBuffer, 3);
|
| 90 | final List<InterfaceVariable> outputs = generateInterfaceVariableList(compiler, spvcResources, spirvBuffer, 4);
|
| 91 | final Int2ReferenceMap<List<Descriptor>> descriptors = new Int2ReferenceArrayMap<>();
|
| 92 |
|
| 93 | for (int i = 0; i < SpvUtil.DESCRIPTOR_TYPES.size(); i++) {
|
| 94 | int descriptorType = SpvUtil.DESCRIPTOR_TYPES.getInt(i);
|
| 95 | descriptors.put(descriptorType, generateDescriptorList(compiler, spvcResources, spirvBuffer, descriptorType));
|
| 96 | }
|
| 97 |
|
| 98 | final List<Descriptor> allDescriptors = descriptors.values().stream().flatMap(Collection::stream).toList();
|
| 99 | final List<PushConstant> pushConstants = generatePushConstantList(compiler, spvcResources);
|
| 100 | this.reflection = new SpvModule.Reflection() {
|
| 101 | @Override
|
| 102 | public List<InterfaceVariable> inputs() {
|
| 103 | return inputs;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public List<InterfaceVariable> outputs() {
|
| 108 | return outputs;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public List<Descriptor> descriptors(final int descriptorType) {
|
| 113 | return descriptors.getOrDefault(descriptorType, List.of());
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public List<Descriptor> descriptors() {
|
| 118 | return allDescriptors;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public List<PushConstant> pushConstants() {
|
| 123 | return pushConstants;
|
| 124 | }
|
| 125 | };
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | private static List<InterfaceVariable> generateInterfaceVariableList(
|
| 130 | final long compiler, final long spvcResources, final IntBuffer spirv, final int resourceType
|
| 131 | ) throws ShaderCompileException {
|
| 132 | ReferenceArrayList<InterfaceVariable> list = new ReferenceArrayList<>();
|
| 133 |
|
| 134 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 135 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 136 | IntBuffer intReturnBuffer = stack.callocInt(1);
|
| 137 | PointerBuffer countPointer = stack.callocPointer(1);
|
| 138 | SpvUtil.throwIfError(
|
| 139 | Spvc.spvc_resources_get_resource_list_for_type(spvcResources, resourceType, pointerReturnBuffer, countPointer), "Couldn't list input variables"
|
| 140 | );
|
| 141 | long spvcList = pointerReturnBuffer.get(0);
|
| 142 | long spvcCount = countPointer.get(0);
|
| 143 | Buffer resources = SpvcReflectedResource.create(spvcList, (int)spvcCount);
|
| 144 |
|
| 145 | for (int i = 0; i < spvcCount; i++) {
|
| 146 | final SpvcReflectedResource resource = resources.get(i);
|
| 147 | final String name = resource.nameString();
|
| 148 | if (!Spvc.spvc_compiler_get_binary_offset_for_decoration(compiler, resource.id(), 30, intReturnBuffer)) {
|
| 149 | throw new ShaderCompileException("Couldn't find byte offset for location decoration of " + name);
|
| 150 | }
|
| 151 |
|
| 152 | final int locationOffset = intReturnBuffer.get(0);
|
| 153 | final SPIRVModule.TypeHandle typeHandle = new SPIRVModule.TypeHandle(Spvc.spvc_compiler_get_type_handle(compiler, resource.type_id()));
|
| 154 | list.add(new InterfaceVariable() {
|
| 155 | public String name() {
|
| 156 | return name;
|
| 157 | }
|
| 158 |
|
| 159 | public Type type() {
|
| 160 | return typeHandle;
|
| 161 | }
|
| 162 |
|
| 163 | public int location() {
|
| 164 | return spirv.get(locationOffset);
|
| 165 | }
|
| 166 |
|
| 167 | public void location(final int location) {
|
| 168 | spirv.put(locationOffset, location);
|
| 169 | }
|
| 170 |
|
| 171 | public int decoration(final int decoration) {
|
| 172 | return Spvc.spvc_compiler_get_decoration(compiler, resource.id(), decoration);
|
| 173 | }
|
| 174 | });
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | return ReferenceLists.unmodifiable(list);
|
| 179 | }
|
| 180 |
|
| 181 | private static List<Descriptor> generateDescriptorList(final long compiler, final long spvcResources, final IntBuffer spirv, final int resourceType) throws ShaderCompileException {
|
| 182 | ReferenceArrayList<Descriptor> list = new ReferenceArrayList<>();
|
| 183 |
|
| 184 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 185 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 186 | IntBuffer intReturnBuffer = stack.callocInt(1);
|
| 187 | PointerBuffer countPointer = stack.callocPointer(1);
|
| 188 | SpvUtil.throwIfError(
|
| 189 | Spvc.spvc_resources_get_resource_list_for_type(spvcResources, resourceType, pointerReturnBuffer, countPointer), "Couldn't list input variables"
|
| 190 | );
|
| 191 | long spvcList = pointerReturnBuffer.get(0);
|
| 192 | long spvcCount = countPointer.get(0);
|
| 193 | Buffer resources = SpvcReflectedResource.create(spvcList, (int)spvcCount);
|
| 194 |
|
| 195 | for (int i = 0; i < spvcCount; i++) {
|
| 196 | SpvcReflectedResource resource = resources.get(i);
|
| 197 | final String name = resource.nameString();
|
| 198 | if (!Spvc.spvc_compiler_get_binary_offset_for_decoration(compiler, resource.id(), 34, intReturnBuffer)) {
|
| 199 | throw new ShaderCompileException("Couldn't find byte offset for set decoration of " + name);
|
| 200 | }
|
| 201 |
|
| 202 | final int setOffset = intReturnBuffer.get(0);
|
| 203 | if (!Spvc.spvc_compiler_get_binary_offset_for_decoration(compiler, resource.id(), 33, intReturnBuffer)) {
|
| 204 | throw new ShaderCompileException("Couldn't find byte offset for binding decoration of " + name);
|
| 205 | }
|
| 206 |
|
| 207 | final SPIRVModule.TypeHandle typeHandle = new SPIRVModule.TypeHandle(Spvc.spvc_compiler_get_type_handle(compiler, resource.type_id()));
|
| 208 | final int bindingOffset = intReturnBuffer.get(0);
|
| 209 | list.add(new Descriptor() {
|
| 210 | public String name() {
|
| 211 | return name;
|
| 212 | }
|
| 213 |
|
| 214 | public Type type() {
|
| 215 | return typeHandle;
|
| 216 | }
|
| 217 |
|
| 218 | public int resourceType() {
|
| 219 | return resourceType;
|
| 220 | }
|
| 221 |
|
| 222 | public int descriptorSetIndex() {
|
| 223 | return spirv.get(setOffset);
|
| 224 | }
|
| 225 |
|
| 226 | public void descriptorSetIndex(final int index) {
|
| 227 | spirv.put(setOffset, index);
|
| 228 | }
|
| 229 |
|
| 230 | public int binding() {
|
| 231 | return spirv.get(bindingOffset);
|
| 232 | }
|
| 233 |
|
| 234 | public void binding(final int binding) {
|
| 235 | spirv.put(bindingOffset, binding);
|
| 236 | }
|
| 237 | });
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | return ReferenceLists.unmodifiable(list);
|
| 242 | }
|
| 243 |
|
| 244 | private static List<PushConstant> generatePushConstantList(final long compiler, final long spvcResources) throws ShaderCompileException {
|
| 245 | ReferenceArrayList<PushConstant> list = new ReferenceArrayList<>();
|
| 246 |
|
| 247 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 248 | PointerBuffer pointerReturnBuffer = stack.callocPointer(1);
|
| 249 | PointerBuffer countPointer = stack.callocPointer(1);
|
| 250 | SpvUtil.throwIfError(Spvc.spvc_resources_get_resource_list_for_type(spvcResources, 9, pointerReturnBuffer, countPointer), "Couldn't list input variables");
|
| 251 | long spvcList = pointerReturnBuffer.get(0);
|
| 252 | long spvcCount = countPointer.get(0);
|
| 253 | Buffer resources = SpvcReflectedResource.create(spvcList, (int)spvcCount);
|
| 254 |
|
| 255 | for (int i = 0; i < spvcCount; i++) {
|
| 256 | SpvcReflectedResource resource = resources.get(i);
|
| 257 | SPIRVModule.TypeHandle typeHandle = new SPIRVModule.TypeHandle(Spvc.spvc_compiler_get_type_handle(compiler, resource.type_id()));
|
| 258 | Spvc.spvc_compiler_get_declared_struct_size(compiler, typeHandle.typeHandle, pointerReturnBuffer);
|
| 259 | int structSize = Math.toIntExact(pointerReturnBuffer.get(0));
|
| 260 | list.add(() -> structSize);
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | return ReferenceLists.unmodifiable(list);
|
| 265 | }
|
| 266 |
|
| 267 | @Environment(EnvType.CLIENT)
|
| 268 | private record TypeHandle(long typeHandle) implements Type {
|
| 269 | public int baseType() {
|
| 270 | return Spvc.spvc_type_get_basetype(this.typeHandle);
|
| 271 | }
|
| 272 |
|
| 273 | public int dimensions() {
|
| 274 | int baseType = this.baseType();
|
| 275 | return baseType != 16 && baseType != 17 ? Integer.MAX_VALUE : Spvc.spvc_type_get_image_dimension(this.typeHandle);
|
| 276 | }
|
| 277 |
|
| 278 | public int vectorSize() {
|
| 279 | return Spvc.spvc_type_get_vector_size(this.typeHandle);
|
| 280 | }
|
| 281 |
|
| 282 | public int arrayDimensions() {
|
| 283 | return Spvc.spvc_type_get_num_array_dimensions(this.typeHandle);
|
| 284 | }
|
| 285 |
|
| 286 | public int arrayLength(final int dimensionIndex) {
|
| 287 | return Spvc.spvc_type_get_array_dimension(this.typeHandle, dimensionIndex);
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 |
|