| 1 | package com.mojang.blaze3d.pipeline;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.renderpearl.api.GpuFormat;
|
| 5 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 6 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 7 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 8 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 9 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.OptionalDouble;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.renderer.RenderPipelines;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public abstract class RenderTarget {
|
| 19 | private static int UNNAMED_RENDER_TARGETS = 0;
|
| 20 | public int width;
|
| 21 | public int height;
|
| 22 | protected final String label;
|
| 23 | protected final @Nullable GpuFormat colorFormat;
|
| 24 | protected final @Nullable GpuFormat depthFormat;
|
| 25 | protected @Nullable GpuTexture colorTexture;
|
| 26 | protected @Nullable GpuTextureView colorTextureView;
|
| 27 | protected @Nullable GpuTexture depthTexture;
|
| 28 | protected @Nullable GpuTextureView depthTextureView;
|
| 29 |
|
| 30 | public RenderTarget(final @Nullable String label, final @Nullable GpuFormat colorFormat, final @Nullable GpuFormat depthFormat) {
|
| 31 | this.label = label == null ? "FBO " + UNNAMED_RENDER_TARGETS++ : label;
|
| 32 | this.colorFormat = colorFormat;
|
| 33 | this.depthFormat = depthFormat;
|
| 34 | }
|
| 35 |
|
| 36 | public void resize(final int width, final int height) {
|
| 37 | RenderSystem.assertOnRenderThread();
|
| 38 | this.destroyBuffers();
|
| 39 | this.createBuffers(width, height);
|
| 40 | }
|
| 41 |
|
| 42 | public void destroyBuffers() {
|
| 43 | RenderSystem.assertOnRenderThread();
|
| 44 | if (this.depthTexture != null) {
|
| 45 | this.depthTexture.close();
|
| 46 | this.depthTexture = null;
|
| 47 | }
|
| 48 |
|
| 49 | if (this.depthTextureView != null) {
|
| 50 | this.depthTextureView.close();
|
| 51 | this.depthTextureView = null;
|
| 52 | }
|
| 53 |
|
| 54 | if (this.colorTexture != null) {
|
| 55 | this.colorTexture.close();
|
| 56 | this.colorTexture = null;
|
| 57 | }
|
| 58 |
|
| 59 | if (this.colorTextureView != null) {
|
| 60 | this.colorTextureView.close();
|
| 61 | this.colorTextureView = null;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | public void copyDepthFrom(final RenderTarget source) {
|
| 66 | RenderSystem.assertOnRenderThread();
|
| 67 | if (this.depthTexture == null) {
|
| 68 | throw new IllegalStateException("Trying to copy depth texture to a RenderTarget without a depth texture");
|
| 69 | }
|
| 70 |
|
| 71 | if (source.depthTexture == null) {
|
| 72 | throw new IllegalStateException("Trying to copy depth texture from a RenderTarget without a depth texture");
|
| 73 | }
|
| 74 |
|
| 75 | RenderSystem.getDevice().createCommandEncoder().copyTextureToTexture(source.depthTexture, this.depthTexture, 0, 0, 0, 0, 0, this.width, this.height);
|
| 76 | }
|
| 77 |
|
| 78 | public void copyColorFrom(final RenderTarget source) {
|
| 79 | RenderSystem.assertOnRenderThread();
|
| 80 | if (this.colorTexture == null) {
|
| 81 | throw new IllegalStateException("Trying to copy color texture to a RenderTarget without a color texture");
|
| 82 | }
|
| 83 |
|
| 84 | if (source.colorTexture == null) {
|
| 85 | throw new IllegalStateException("Trying to copy color texture from a RenderTarget without a color texture");
|
| 86 | }
|
| 87 |
|
| 88 | RenderSystem.getDevice().createCommandEncoder().copyTextureToTexture(source.colorTexture, this.colorTexture, 0, 0, 0, 0, 0, this.width, this.height);
|
| 89 | }
|
| 90 |
|
| 91 | public void createBuffers(final int width, final int height) {
|
| 92 | RenderSystem.assertOnRenderThread();
|
| 93 | GpuDevice device = RenderSystem.getDevice();
|
| 94 | int maxTextureSize = device.getDeviceInfo().limits().maxTextureSize();
|
| 95 | if (width > 0 && width <= maxTextureSize && height > 0 && height <= maxTextureSize) {
|
| 96 | this.width = width;
|
| 97 | this.height = height;
|
| 98 | if (this.depthFormat != null) {
|
| 99 | this.depthTexture = device.createTexture(() -> this.label + " / Depth", 15, this.depthFormat, width, height, 1, 1);
|
| 100 | this.depthTextureView = device.createTextureView(this.depthTexture);
|
| 101 | }
|
| 102 |
|
| 103 | if (this.colorFormat != null) {
|
| 104 | this.colorTexture = device.createTexture(() -> this.label + " / Color", 15, this.colorFormat, width, height, 1, 1);
|
| 105 | this.colorTextureView = device.createTextureView(this.colorTexture);
|
| 106 | }
|
| 107 | } else {
|
| 108 | throw new IllegalArgumentException("Window " + width + "x" + height + " size out of bounds (max. size: " + maxTextureSize + ")");
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | public void blitAndBlendToTexture(final GpuTextureView output, final GpuTextureView outputDepth) {
|
| 113 | RenderSystem.assertOnRenderThread();
|
| 114 |
|
| 115 | try (RenderPass renderPass = RenderSystem.getDevice()
|
| 116 | .createCommandEncoder()
|
| 117 | .createRenderPass(() -> "Blit render target", output, Optional.empty(), outputDepth, OptionalDouble.empty())) {
|
| 118 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(RenderPipelines.ENTITY_OUTLINE_BLIT));
|
| 119 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 120 | renderPass.setUniform("InSampler", this.colorTextureView, RenderSystem.getSamplerCache().getClampToEdge(FilterMode.NEAREST));
|
| 121 | renderPass.draw(3, 1, 0, 0);
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public @Nullable GpuTexture getColorTexture() {
|
| 126 | return this.colorTexture;
|
| 127 | }
|
| 128 |
|
| 129 | public @Nullable GpuTextureView getColorTextureView() {
|
| 130 | return this.colorTextureView;
|
| 131 | }
|
| 132 |
|
| 133 | public @Nullable GpuTexture getDepthTexture() {
|
| 134 | return this.depthTexture;
|
| 135 | }
|
| 136 |
|
| 137 | public @Nullable GpuTextureView getDepthTextureView() {
|
| 138 | return this.depthTextureView;
|
| 139 | }
|
| 140 |
|
| 141 | public boolean hasDepth() {
|
| 142 | return this.depthFormat != null;
|
| 143 | }
|
| 144 | }
|
| 145 |
|