| 1 | package com.mojang.blaze3d.pipeline;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 5 | import com.mojang.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.device.GpuOutOfMemoryException;
|
| 7 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Objects;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class MainTarget extends RenderTarget {
|
| 16 | public static final int DEFAULT_WIDTH = 854;
|
| 17 | public static final int DEFAULT_HEIGHT = 480;
|
| 18 | private static final MainTarget.Dimension DEFAULT_DIMENSIONS = new MainTarget.Dimension(854, 480);
|
| 19 |
|
| 20 | public MainTarget(final int desiredWidth, final int desiredHeight) {
|
| 21 | super("Main", GpuFormat.RGBA8_UNORM, GpuFormat.D32_FLOAT);
|
| 22 | this.createFrameBuffer(desiredWidth, desiredHeight);
|
| 23 | }
|
| 24 |
|
| 25 | private void createFrameBuffer(final int desiredWidth, final int desiredHeight) {
|
| 26 | MainTarget.Dimension allocatedDimensions = this.allocateAttachments(desiredWidth, desiredHeight);
|
| 27 | if (this.colorTexture != null && this.depthTexture != null) {
|
| 28 | this.width = allocatedDimensions.width;
|
| 29 | this.height = allocatedDimensions.height;
|
| 30 | } else {
|
| 31 | throw new IllegalStateException("Missing color and/or depth textures");
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | private MainTarget.Dimension allocateAttachments(final int width, final int height) {
|
| 36 | RenderSystem.assertOnRenderThread();
|
| 37 |
|
| 38 | for (MainTarget.Dimension dimension : MainTarget.Dimension.listWithFallback(width, height)) {
|
| 39 | if (this.colorTexture != null) {
|
| 40 | this.colorTexture.close();
|
| 41 | this.colorTexture = null;
|
| 42 | }
|
| 43 |
|
| 44 | if (this.colorTextureView != null) {
|
| 45 | this.colorTextureView.close();
|
| 46 | this.colorTextureView = null;
|
| 47 | }
|
| 48 |
|
| 49 | if (this.depthTexture != null) {
|
| 50 | this.depthTexture.close();
|
| 51 | this.depthTexture = null;
|
| 52 | }
|
| 53 |
|
| 54 | if (this.depthTextureView != null) {
|
| 55 | this.depthTextureView.close();
|
| 56 | this.depthTextureView = null;
|
| 57 | }
|
| 58 |
|
| 59 | this.colorTexture = this.allocateColorAttachment(dimension);
|
| 60 | this.depthTexture = this.allocateDepthAttachment(dimension);
|
| 61 | if (this.colorTexture != null && this.depthTexture != null) {
|
| 62 | this.colorTextureView = RenderSystem.getDevice().createTextureView(this.colorTexture);
|
| 63 | this.depthTextureView = RenderSystem.getDevice().createTextureView(this.depthTexture);
|
| 64 | return dimension;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | throw new RuntimeException(
|
| 69 | "Unrecoverable GL_OUT_OF_MEMORY ("
|
| 70 | + (this.colorTexture == null ? "missing color" : "have color")
|
| 71 | + ", "
|
| 72 | + (this.depthTexture == null ? "missing depth" : "have depth")
|
| 73 | + ")"
|
| 74 | );
|
| 75 | }
|
| 76 |
|
| 77 | private @Nullable GpuTexture allocateColorAttachment(final MainTarget.Dimension dimension) {
|
| 78 | try {
|
| 79 | return RenderSystem.getDevice().createTexture(() -> this.label + " / Color", 15, this.colorFormat, dimension.width, dimension.height, 1, 1);
|
| 80 | } catch (GpuOutOfMemoryException ignored) {
|
| 81 | return null;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | private @Nullable GpuTexture allocateDepthAttachment(final MainTarget.Dimension dimension) {
|
| 86 | try {
|
| 87 | return RenderSystem.getDevice().createTexture(() -> this.label + " / Depth", 15, this.depthFormat, dimension.width, dimension.height, 1, 1);
|
| 88 | } catch (GpuOutOfMemoryException ignored) {
|
| 89 | return null;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Environment(EnvType.CLIENT)
|
| 94 | private static class Dimension {
|
| 95 | public final int width;
|
| 96 | public final int height;
|
| 97 |
|
| 98 | private Dimension(final int width, final int height) {
|
| 99 | this.width = width;
|
| 100 | this.height = height;
|
| 101 | }
|
| 102 |
|
| 103 | private static List<MainTarget.Dimension> listWithFallback(final int width, final int height) {
|
| 104 | RenderSystem.assertOnRenderThread();
|
| 105 | int maxTextureSize = RenderSystem.getDevice().getDeviceInfo().limits().maxTextureSize();
|
| 106 | return width > 0 && width <= maxTextureSize && height > 0 && height <= maxTextureSize
|
| 107 | ? ImmutableList.of(new MainTarget.Dimension(width, height), MainTarget.DEFAULT_DIMENSIONS)
|
| 108 | : ImmutableList.of(MainTarget.DEFAULT_DIMENSIONS);
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public boolean equals(final Object other) {
|
| 113 | if (this == other) {
|
| 114 | return true;
|
| 115 | } else if (other != null && this.getClass() == other.getClass()) {
|
| 116 | MainTarget.Dimension that = (MainTarget.Dimension)other;
|
| 117 | return this.width == that.width && this.height == that.height;
|
| 118 | } else {
|
| 119 | return false;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public int hashCode() {
|
| 125 | return Objects.hash(this.width, this.height);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public String toString() {
|
| 130 | return this.width + "x" + this.height;
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|