| 1 | package com.mojang.renderpearl.api.commands;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 4 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 5 | import it.unimi.dsi.fastutil.objects.ReferenceLists;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.OptionalDouble;
|
| 10 | import java.util.function.Supplier;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import org.joml.Vector4fc;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public record RenderPassDescriptor(
|
| 18 | Supplier<String> label,
|
| 19 | List<RenderPassDescriptor.@Nullable Attachment<Optional<Vector4fc>>> colorAttachments,
|
| 20 | RenderPassDescriptor.@Nullable Attachment<OptionalDouble> depthAttachment,
|
| 21 | RenderPass.RenderArea renderArea
|
| 22 | ) {
|
| 23 | public RenderPassDescriptor {
|
| 24 | colorAttachments = ReferenceLists.unmodifiable(new ReferenceArrayList<>(colorAttachments));
|
| 25 | }
|
| 26 |
|
| 27 | public static RenderPassDescriptor.Builder builder(final Supplier<String> label) {
|
| 28 | return new RenderPassDescriptor.Builder(label);
|
| 29 | }
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public record Attachment<T>(GpuTextureView textureView, T clearValue) {
|
| 33 | }
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | public static class Builder {
|
| 37 | private final Supplier<String> label;
|
| 38 | private final List<RenderPassDescriptor.@Nullable Attachment<Optional<Vector4fc>>> colorAttachments = new ArrayList<>();
|
| 39 | private RenderPassDescriptor.@Nullable Attachment<OptionalDouble> depthAttachment;
|
| 40 | private RenderPass.@Nullable RenderArea renderArea;
|
| 41 |
|
| 42 | private Builder(final Supplier<String> label) {
|
| 43 | this.label = label;
|
| 44 | }
|
| 45 |
|
| 46 | public RenderPassDescriptor.Builder withColorAttachment(final GpuTextureView textureView) {
|
| 47 | this.colorAttachments.add(new RenderPassDescriptor.Attachment<>(textureView, Optional.empty()));
|
| 48 | return this;
|
| 49 | }
|
| 50 |
|
| 51 | public RenderPassDescriptor.Builder withColorAttachment(final GpuTextureView textureView, final Optional<Vector4fc> clearValue) {
|
| 52 | this.colorAttachments.add(new RenderPassDescriptor.Attachment<>(textureView, clearValue));
|
| 53 | return this;
|
| 54 | }
|
| 55 |
|
| 56 | public RenderPassDescriptor.Builder withUnusedColorAttachment() {
|
| 57 | this.colorAttachments.add(null);
|
| 58 | return this;
|
| 59 | }
|
| 60 |
|
| 61 | public RenderPassDescriptor.Builder withDepthAttachment(final GpuTextureView textureView) {
|
| 62 | this.depthAttachment = new RenderPassDescriptor.Attachment<>(textureView, OptionalDouble.empty());
|
| 63 | return this;
|
| 64 | }
|
| 65 |
|
| 66 | public RenderPassDescriptor.Builder withDepthAttachment(final GpuTextureView textureView, final OptionalDouble clearValue) {
|
| 67 | this.depthAttachment = new RenderPassDescriptor.Attachment<>(textureView, clearValue);
|
| 68 | return this;
|
| 69 | }
|
| 70 |
|
| 71 | public RenderPassDescriptor.Builder withRenderArea(final RenderPass.RenderArea renderArea) {
|
| 72 | this.renderArea = renderArea;
|
| 73 | return this;
|
| 74 | }
|
| 75 |
|
| 76 | public RenderPassDescriptor build() {
|
| 77 | RenderPass.RenderArea renderArea = this.renderArea != null ? this.renderArea : defaultRenderArea(this.colorAttachments, this.depthAttachment);
|
| 78 | return new RenderPassDescriptor(this.label, this.colorAttachments, this.depthAttachment, renderArea);
|
| 79 | }
|
| 80 |
|
| 81 | private static RenderPass.RenderArea defaultRenderArea(
|
| 82 | final List<RenderPassDescriptor.@Nullable Attachment<Optional<Vector4fc>>> colorAttachments,
|
| 83 | final RenderPassDescriptor.@Nullable Attachment<OptionalDouble> depthAttachment
|
| 84 | ) {
|
| 85 | int width = 0;
|
| 86 | int height = 0;
|
| 87 | if (!colorAttachments.isEmpty()) {
|
| 88 | for (RenderPassDescriptor.Attachment<Optional<Vector4fc>> colorAttachment : colorAttachments) {
|
| 89 | if (colorAttachment != null) {
|
| 90 | GpuTextureView textureView = colorAttachment.textureView();
|
| 91 | width = textureView.getWidth(0);
|
| 92 | height = textureView.getHeight(0);
|
| 93 | }
|
| 94 | }
|
| 95 | } else if (depthAttachment != null) {
|
| 96 | width = depthAttachment.textureView().getWidth(0);
|
| 97 | height = depthAttachment.textureView().getHeight(0);
|
| 98 | }
|
| 99 |
|
| 100 | return new RenderPass.RenderArea(0, 0, width, height);
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|