| 1 | package com.mojang.renderpearl.api.pipeline;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.vertex.VertexFormat;
|
| 4 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 5 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 6 | import it.unimi.dsi.fastutil.objects.ReferenceLists;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.EnumMap;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.Map.Entry;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.fabricmc.fabric.api.client.rendering.v1.FabricRenderPipeline;
|
| 20 | import net.minecraft.SharedConstants;
|
| 21 | import net.minecraft.client.renderer.ShaderDefines;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class RenderPipeline implements FabricRenderPipeline {
|
| 27 | private final Identifier location;
|
| 28 | private final Map<ShaderType, Identifier> shaders;
|
| 29 | private final ShaderDefines shaderDefines;
|
| 30 | private final List<BindGroupLayout> bindGroupLayouts;
|
| 31 | private final @Nullable DepthStencilState depthStencilState;
|
| 32 | private final PolygonMode polygonMode;
|
| 33 | private final boolean cull;
|
| 34 | private final List<@Nullable ColorTargetState> colorTargetStates;
|
| 35 | private final List<@Nullable VertexFormat> vertexFormatPerBuffer;
|
| 36 | private final PrimitiveTopology primitiveTopology;
|
| 37 | private final int pushConstantSize;
|
| 38 | private final int sortKey;
|
| 39 | private static int sortKeySeed;
|
| 40 |
|
| 41 | protected RenderPipeline(
|
| 42 | final Identifier location,
|
| 43 | final Map<ShaderType, Identifier> shaders,
|
| 44 | final ShaderDefines shaderDefines,
|
| 45 | final Collection<BindGroupLayout> bindGroupLayouts,
|
| 46 | final @Nullable ColorTargetState[] colorTargetStates,
|
| 47 | final @Nullable DepthStencilState depthStencilState,
|
| 48 | final PolygonMode polygonMode,
|
| 49 | final boolean cull,
|
| 50 | final @Nullable VertexFormat[] vertexFormatPerBuffer,
|
| 51 | final PrimitiveTopology primitiveTopology,
|
| 52 | final int pushConstantSize,
|
| 53 | final int sortKey
|
| 54 | ) {
|
| 55 | this.location = location;
|
| 56 | this.shaders = Collections.unmodifiableMap(new EnumMap<>(shaders));
|
| 57 | this.shaderDefines = shaderDefines;
|
| 58 | this.bindGroupLayouts = List.copyOf(bindGroupLayouts);
|
| 59 | this.depthStencilState = depthStencilState;
|
| 60 | this.polygonMode = polygonMode;
|
| 61 | this.cull = cull;
|
| 62 | this.colorTargetStates = ReferenceLists.unmodifiable(new ReferenceArrayList<>(colorTargetStates));
|
| 63 | this.vertexFormatPerBuffer = ReferenceLists.unmodifiable(new ReferenceArrayList<>(vertexFormatPerBuffer));
|
| 64 | this.primitiveTopology = primitiveTopology;
|
| 65 | this.pushConstantSize = pushConstantSize;
|
| 66 | this.sortKey = sortKey;
|
| 67 | }
|
| 68 |
|
| 69 | public int getSortKey() {
|
| 70 | return SharedConstants.DEBUG_SHUFFLE_UI_RENDERING_ORDER ? super.hashCode() * (sortKeySeed + 1) : this.sortKey;
|
| 71 | }
|
| 72 |
|
| 73 | public static void updateSortKeySeed() {
|
| 74 | sortKeySeed = Math.round(100000.0F * (float)Math.random());
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public String toString() {
|
| 79 | return this.location.toString();
|
| 80 | }
|
| 81 |
|
| 82 | public PolygonMode getPolygonMode() {
|
| 83 | return this.polygonMode;
|
| 84 | }
|
| 85 |
|
| 86 | public boolean isCull() {
|
| 87 | return this.cull;
|
| 88 | }
|
| 89 |
|
| 90 | public List<@Nullable ColorTargetState> getColorTargetStates() {
|
| 91 | return this.colorTargetStates;
|
| 92 | }
|
| 93 |
|
| 94 | public @Nullable DepthStencilState getDepthStencilState() {
|
| 95 | return this.depthStencilState;
|
| 96 | }
|
| 97 |
|
| 98 | public Identifier getLocation() {
|
| 99 | return this.location;
|
| 100 | }
|
| 101 |
|
| 102 | public List<@Nullable VertexFormat> getVertexFormatBindings() {
|
| 103 | return this.vertexFormatPerBuffer;
|
| 104 | }
|
| 105 |
|
| 106 | public @Nullable VertexFormat getVertexFormatBinding(final int bindingIndex) {
|
| 107 | return this.vertexFormatPerBuffer.get(bindingIndex);
|
| 108 | }
|
| 109 |
|
| 110 | public PrimitiveTopology getPrimitiveTopology() {
|
| 111 | return this.primitiveTopology;
|
| 112 | }
|
| 113 |
|
| 114 | public Map<ShaderType, Identifier> getShaders() {
|
| 115 | return this.shaders;
|
| 116 | }
|
| 117 |
|
| 118 | public ShaderDefines getShaderDefines() {
|
| 119 | return this.shaderDefines;
|
| 120 | }
|
| 121 |
|
| 122 | public List<BindGroupLayout> getBindGroupLayouts() {
|
| 123 | return this.bindGroupLayouts;
|
| 124 | }
|
| 125 |
|
| 126 | public boolean wantsDepthTexture() {
|
| 127 | return this.depthStencilState != null;
|
| 128 | }
|
| 129 |
|
| 130 | public int pushConstantSize() {
|
| 131 | return this.pushConstantSize;
|
| 132 | }
|
| 133 |
|
| 134 | public static RenderPipeline.Builder builder(final RenderPipeline.Snippet... snippets) {
|
| 135 | RenderPipeline.Builder builder = new RenderPipeline.Builder();
|
| 136 |
|
| 137 | for (RenderPipeline.Snippet snippet : snippets) {
|
| 138 | builder.withSnippet(snippet);
|
| 139 | }
|
| 140 |
|
| 141 | return builder;
|
| 142 | }
|
| 143 |
|
| 144 | @Environment(EnvType.CLIENT)
|
| 145 | public static class Builder implements net.fabricmc.fabric.api.client.rendering.v1.FabricRenderPipeline.Builder {
|
| 146 | private static int nextPipelineSortKey;
|
| 147 | private Optional<Identifier> location = Optional.empty();
|
| 148 | private final Map<ShaderType, Identifier> shaders = new EnumMap<>(ShaderType.class);
|
| 149 | private Optional<ShaderDefines.Builder> definesBuilder = Optional.empty();
|
| 150 | private Optional<Set<BindGroupLayout>> bindGroupLayouts = Optional.empty();
|
| 151 | private Optional<DepthStencilState> depthStencilState = Optional.empty();
|
| 152 | private Optional<PolygonMode> polygonMode = Optional.empty();
|
| 153 | private Optional<Boolean> cull = Optional.empty();
|
| 154 | private final @Nullable ColorTargetState[] colorTargetStates = new ColorTargetState[8];
|
| 155 | private int activeColorTargetStateCount;
|
| 156 | private final @Nullable VertexFormat[] vertexFormatPerBuffer = new VertexFormat[16];
|
| 157 | private Optional<PrimitiveTopology> primitiveTopology = Optional.empty();
|
| 158 | private int pushConstantSize = 0;
|
| 159 |
|
| 160 | private Builder() {
|
| 161 | }
|
| 162 |
|
| 163 | public RenderPipeline.Builder withLocation(final String location) {
|
| 164 | this.location = Optional.of(Identifier.withDefaultNamespace(location));
|
| 165 | return this;
|
| 166 | }
|
| 167 |
|
| 168 | public RenderPipeline.Builder withLocation(final Identifier location) {
|
| 169 | this.location = Optional.of(location);
|
| 170 | return this;
|
| 171 | }
|
| 172 |
|
| 173 | public RenderPipeline.Builder withFragmentShader(final String fragmentShader) {
|
| 174 | return this.withFragmentShader(Identifier.withDefaultNamespace(fragmentShader));
|
| 175 | }
|
| 176 |
|
| 177 | public RenderPipeline.Builder withFragmentShader(final Identifier fragmentShader) {
|
| 178 | this.shaders.put(ShaderType.FRAGMENT, fragmentShader);
|
| 179 | return this;
|
| 180 | }
|
| 181 |
|
| 182 | public RenderPipeline.Builder withVertexShader(final String vertexShader) {
|
| 183 | return this.withVertexShader(Identifier.withDefaultNamespace(vertexShader));
|
| 184 | }
|
| 185 |
|
| 186 | public RenderPipeline.Builder withVertexShader(final Identifier vertexShader) {
|
| 187 | this.shaders.put(ShaderType.VERTEX, vertexShader);
|
| 188 | return this;
|
| 189 | }
|
| 190 |
|
| 191 | public RenderPipeline.Builder withShaderDefine(final String key) {
|
| 192 | if (this.definesBuilder.isEmpty()) {
|
| 193 | this.definesBuilder = Optional.of(ShaderDefines.builder());
|
| 194 | }
|
| 195 |
|
| 196 | this.definesBuilder.get().define(key);
|
| 197 | return this;
|
| 198 | }
|
| 199 |
|
| 200 | public RenderPipeline.Builder withShaderDefine(final String key, final int value) {
|
| 201 | if (this.definesBuilder.isEmpty()) {
|
| 202 | this.definesBuilder = Optional.of(ShaderDefines.builder());
|
| 203 | }
|
| 204 |
|
| 205 | this.definesBuilder.get().define(key, value);
|
| 206 | return this;
|
| 207 | }
|
| 208 |
|
| 209 | public RenderPipeline.Builder withShaderDefine(final String key, final float value) {
|
| 210 | if (this.definesBuilder.isEmpty()) {
|
| 211 | this.definesBuilder = Optional.of(ShaderDefines.builder());
|
| 212 | }
|
| 213 |
|
| 214 | this.definesBuilder.get().define(key, value);
|
| 215 | return this;
|
| 216 | }
|
| 217 |
|
| 218 | public RenderPipeline.Builder withBindGroupLayout(final BindGroupLayout bindGroupLayout) {
|
| 219 | if (this.bindGroupLayouts.isEmpty()) {
|
| 220 | this.bindGroupLayouts = Optional.of(new ObjectOpenHashSet<>());
|
| 221 | }
|
| 222 |
|
| 223 | this.bindGroupLayouts.get().add(bindGroupLayout);
|
| 224 | return this;
|
| 225 | }
|
| 226 |
|
| 227 | public RenderPipeline.Builder withPolygonMode(final PolygonMode polygonMode) {
|
| 228 | this.polygonMode = Optional.of(polygonMode);
|
| 229 | return this;
|
| 230 | }
|
| 231 |
|
| 232 | public RenderPipeline.Builder withCull(final boolean cull) {
|
| 233 | this.cull = Optional.of(cull);
|
| 234 | return this;
|
| 235 | }
|
| 236 |
|
| 237 | public RenderPipeline.Builder withColorTargetState(final int index, final ColorTargetState colorTargetState) {
|
| 238 | this.colorTargetStates[index] = colorTargetState;
|
| 239 | this.activeColorTargetStateCount = Math.max(this.activeColorTargetStateCount, index + 1);
|
| 240 | return this;
|
| 241 | }
|
| 242 |
|
| 243 | public RenderPipeline.Builder withColorTargetStates(final int startIindex, final int endIndex, final Supplier<ColorTargetState> colorTargetState) {
|
| 244 | for (int i = startIindex; i <= endIndex; i++) {
|
| 245 | this.colorTargetStates[i] = colorTargetState.get();
|
| 246 | this.activeColorTargetStateCount = Math.max(this.activeColorTargetStateCount, i + 1);
|
| 247 | }
|
| 248 |
|
| 249 | return this;
|
| 250 | }
|
| 251 |
|
| 252 | public RenderPipeline.Builder withUnusedColorTargetState(final int index) {
|
| 253 | this.colorTargetStates[index] = null;
|
| 254 | this.activeColorTargetStateCount = Math.max(this.activeColorTargetStateCount, index + 1);
|
| 255 | return this;
|
| 256 | }
|
| 257 |
|
| 258 | public RenderPipeline.Builder withColorTargetState(final ColorTargetState colorTargetState) {
|
| 259 | return this.withColorTargetState(0, colorTargetState);
|
| 260 | }
|
| 261 |
|
| 262 | public RenderPipeline.Builder withDepthStencilState(final DepthStencilState depthStencilState) {
|
| 263 | this.depthStencilState = Optional.of(depthStencilState);
|
| 264 | return this;
|
| 265 | }
|
| 266 |
|
| 267 | public RenderPipeline.Builder withDepthStencilState(final Optional<DepthStencilState> depthStencilState) {
|
| 268 | this.depthStencilState = depthStencilState;
|
| 269 | return this;
|
| 270 | }
|
| 271 |
|
| 272 | public RenderPipeline.Builder withVertexBinding(final int bindingIndex, final VertexFormat vertexFormat) {
|
| 273 | this.vertexFormatPerBuffer[bindingIndex] = vertexFormat;
|
| 274 | return this;
|
| 275 | }
|
| 276 |
|
| 277 | public RenderPipeline.Builder withPrimitiveTopology(final PrimitiveTopology primitiveTopology) {
|
| 278 | this.primitiveTopology = Optional.of(primitiveTopology);
|
| 279 | return this;
|
| 280 | }
|
| 281 |
|
| 282 | public RenderPipeline.Builder withPushConstantSize(final int pushConstantSize) {
|
| 283 | this.pushConstantSize = pushConstantSize;
|
| 284 | return this;
|
| 285 | }
|
| 286 |
|
| 287 | public RenderPipeline.Builder withSnippet(final RenderPipeline.Snippet snippet) {
|
| 288 | this.shaders.putAll(snippet.shaders);
|
| 289 | if (snippet.shaderDefines.isPresent()) {
|
| 290 | if (this.definesBuilder.isEmpty()) {
|
| 291 | this.definesBuilder = Optional.of(ShaderDefines.builder());
|
| 292 | }
|
| 293 |
|
| 294 | ShaderDefines snippetDefines = snippet.shaderDefines.get();
|
| 295 |
|
| 296 | for (Entry<String, String> snippetValue : snippetDefines.values().entrySet()) {
|
| 297 | this.definesBuilder.get().define(snippetValue.getKey(), snippetValue.getValue());
|
| 298 | }
|
| 299 |
|
| 300 | for (String flag : snippetDefines.flags()) {
|
| 301 | this.definesBuilder.get().define(flag);
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | snippet.bindGroupLayouts.ifPresent(snippetLayouts -> {
|
| 306 | if (this.bindGroupLayouts.isPresent()) {
|
| 307 | this.bindGroupLayouts.get().addAll((Collection<? extends BindGroupLayout>)snippetLayouts);
|
| 308 | } else {
|
| 309 | this.bindGroupLayouts = Optional.of(new ObjectOpenHashSet<>((Collection<? extends BindGroupLayout>)snippetLayouts));
|
| 310 | }
|
| 311 | });
|
| 312 | if (snippet.depthStencilState.isPresent()) {
|
| 313 | this.depthStencilState = snippet.depthStencilState;
|
| 314 | }
|
| 315 |
|
| 316 | if (snippet.cull.isPresent()) {
|
| 317 | this.cull = snippet.cull;
|
| 318 | }
|
| 319 |
|
| 320 | for (int i = 0; i < snippet.activeColorTargetStateCount; i++) {
|
| 321 | if (this.colorTargetStates[i] == null && snippet.colorTargetStates[i] != null) {
|
| 322 | this.colorTargetStates[i] = snippet.colorTargetStates[i];
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | this.activeColorTargetStateCount = Math.max(this.activeColorTargetStateCount, snippet.activeColorTargetStateCount);
|
| 327 |
|
| 328 | for (int i = 0; i < snippet.vertexFormatPerBuffer.length; i++) {
|
| 329 | VertexFormat vertexFormat = snippet.vertexFormatPerBuffer[i];
|
| 330 | if (vertexFormat != null) {
|
| 331 | this.vertexFormatPerBuffer[i] = vertexFormat;
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | if (snippet.vertexFormatMode.isPresent()) {
|
| 336 | this.primitiveTopology = snippet.vertexFormatMode;
|
| 337 | }
|
| 338 |
|
| 339 | if (snippet.polygonMode.isPresent()) {
|
| 340 | this.polygonMode = snippet.polygonMode;
|
| 341 | }
|
| 342 |
|
| 343 | this.pushConstantSize = Math.max(this.pushConstantSize, snippet.pushConstantSize);
|
| 344 | return this;
|
| 345 | }
|
| 346 |
|
| 347 | public RenderPipeline.Snippet buildSnippet() {
|
| 348 | return new RenderPipeline.Snippet(
|
| 349 | Collections.unmodifiableMap(new EnumMap<>(this.shaders)),
|
| 350 | this.definesBuilder.map(ShaderDefines.Builder::build),
|
| 351 | this.bindGroupLayouts.map(List::copyOf),
|
| 352 | this.colorTargetStates,
|
| 353 | this.activeColorTargetStateCount,
|
| 354 | this.depthStencilState,
|
| 355 | this.polygonMode,
|
| 356 | this.cull,
|
| 357 | this.vertexFormatPerBuffer,
|
| 358 | this.primitiveTopology,
|
| 359 | this.pushConstantSize
|
| 360 | );
|
| 361 | }
|
| 362 |
|
| 363 | public RenderPipeline build() {
|
| 364 | if (this.location.isEmpty()) {
|
| 365 | throw new IllegalStateException("Missing location");
|
| 366 | }
|
| 367 |
|
| 368 | if (!this.shaders.containsKey(ShaderType.VERTEX)) {
|
| 369 | throw new IllegalStateException("Missing vertex shader");
|
| 370 | }
|
| 371 |
|
| 372 | if (!this.shaders.containsKey(ShaderType.FRAGMENT)) {
|
| 373 | throw new IllegalStateException("Missing fragment shader");
|
| 374 | }
|
| 375 |
|
| 376 | if (this.primitiveTopology.isEmpty()) {
|
| 377 | throw new IllegalStateException("Missing primitive topology");
|
| 378 | }
|
| 379 |
|
| 380 | ColorTargetState[] activeColorTargetStates;
|
| 381 | if (this.activeColorTargetStateCount == 0) {
|
| 382 | activeColorTargetStates = new ColorTargetState[0];
|
| 383 | } else {
|
| 384 | activeColorTargetStates = Arrays.copyOf(this.colorTargetStates, this.activeColorTargetStateCount);
|
| 385 | Optional<BlendFunction> lastBlend = Optional.empty();
|
| 386 |
|
| 387 | for (ColorTargetState activeColorTargetState : activeColorTargetStates) {
|
| 388 | if (activeColorTargetState != null) {
|
| 389 | Optional<BlendFunction> currentBlend = activeColorTargetState.blendFunction();
|
| 390 | if (currentBlend.isPresent()) {
|
| 391 | if (lastBlend.isEmpty()) {
|
| 392 | lastBlend = currentBlend;
|
| 393 | } else if (!currentBlend.equals(lastBlend)) {
|
| 394 | throw new IllegalStateException("Blend functions must currently be the same for all color targets");
|
| 395 | }
|
| 396 | }
|
| 397 | }
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | int boundVertexAttribCount = 0;
|
| 402 |
|
| 403 | for (VertexFormat bindings : this.vertexFormatPerBuffer) {
|
| 404 | if (bindings != null) {
|
| 405 | boundVertexAttribCount += bindings.getElements().size();
|
| 406 | }
|
| 407 | }
|
| 408 |
|
| 409 | if (boundVertexAttribCount > 16) {
|
| 410 | throw new IllegalStateException("Binding more than 16 vertex attributes is not supported");
|
| 411 | } else if (this.pushConstantSize > 128) {
|
| 412 | throw new IllegalStateException("Maximum push constant size is 128 bytes");
|
| 413 | } else {
|
| 414 | return new RenderPipeline(
|
| 415 | this.location.get(),
|
| 416 | this.shaders,
|
| 417 | this.definesBuilder.orElse(ShaderDefines.builder()).build(),
|
| 418 | this.bindGroupLayouts.orElse(Collections.emptySet()),
|
| 419 | activeColorTargetStates,
|
| 420 | this.depthStencilState.orElse(null),
|
| 421 | this.polygonMode.orElse(PolygonMode.FILL),
|
| 422 | this.cull.orElse(true),
|
| 423 | this.vertexFormatPerBuffer,
|
| 424 | this.primitiveTopology.get(),
|
| 425 | this.pushConstantSize,
|
| 426 | nextPipelineSortKey++
|
| 427 | );
|
| 428 | }
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | @Environment(EnvType.CLIENT)
|
| 433 | public record Snippet(
|
| 434 | Map<ShaderType, Identifier> shaders,
|
| 435 | Optional<ShaderDefines> shaderDefines,
|
| 436 | Optional<List<BindGroupLayout>> bindGroupLayouts,
|
| 437 | @Nullable ColorTargetState[] colorTargetStates,
|
| 438 | int activeColorTargetStateCount,
|
| 439 | Optional<DepthStencilState> depthStencilState,
|
| 440 | Optional<PolygonMode> polygonMode,
|
| 441 | Optional<Boolean> cull,
|
| 442 | @Nullable VertexFormat[] vertexFormatPerBuffer,
|
| 443 | Optional<PrimitiveTopology> vertexFormatMode,
|
| 444 | int pushConstantSize
|
| 445 | ) implements net.fabricmc.fabric.api.client.rendering.v1.FabricRenderPipeline.Snippet {
|
| 446 | }
|
| 447 | }
|
| 448 |
|