| 1 | package com.mojang.blaze3d.resource;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.pipeline.RenderTarget;
|
| 4 | import com.mojang.blaze3d.pipeline.TextureTarget;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.renderpearl.api.GpuFormat;
|
| 7 | import java.util.Objects;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import org.joml.Vector4f;
|
| 11 | import org.joml.Vector4fc;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public record RenderTargetDescriptor(
|
| 16 | int width, int height, RenderTargetDescriptor.@Nullable TextureProperties color, RenderTargetDescriptor.@Nullable TextureProperties depth
|
| 17 | ) implements ResourceDescriptor<RenderTarget> {
|
| 18 | public RenderTarget allocate() {
|
| 19 | return new TextureTarget(null, this.width, this.height, this.color != null ? this.color.format : null, this.depth != null ? this.depth.format : null);
|
| 20 | }
|
| 21 |
|
| 22 | public void prepare(final RenderTarget resource) {
|
| 23 | if (this.color != null && this.depth != null && this.color.clearColor != null && this.depth.clearColor != null) {
|
| 24 | RenderSystem.getDevice()
|
| 25 | .createCommandEncoder()
|
| 26 | .clearColorAndDepthTextures(resource.getColorTexture(), this.color.clearColor, resource.getDepthTexture(), this.depth.clearColor.x());
|
| 27 | } else if (this.color != null && this.color.clearColor != null) {
|
| 28 | RenderSystem.getDevice().createCommandEncoder().clearColorTexture(resource.getColorTexture(), this.color.clearColor);
|
| 29 | } else if (this.depth != null && this.depth.clearColor != null) {
|
| 30 | RenderSystem.getDevice().createCommandEncoder().clearDepthTexture(resource.getDepthTexture(), this.depth.clearColor.x());
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public void free(final RenderTarget resource) {
|
| 35 | resource.destroyBuffers();
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public boolean canUsePhysicalResource(final ResourceDescriptor<?> other) {
|
| 40 | return !(other instanceof RenderTargetDescriptor descriptor)
|
| 41 | ? false
|
| 42 | : this.width == descriptor.width
|
| 43 | && this.height == descriptor.height
|
| 44 | && Objects.equals(this.color, descriptor.color)
|
| 45 | && Objects.equals(this.depth, descriptor.depth);
|
| 46 | }
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | public record TextureProperties(@Nullable Vector4fc clearColor, GpuFormat format) {
|
| 50 | public static final RenderTargetDescriptor.TextureProperties DEFAULT_DEPTH = new RenderTargetDescriptor.TextureProperties(
|
| 51 | new Vector4f(0.0F, 0.0F, 0.0F, 0.0F), GpuFormat.D32_FLOAT
|
| 52 | );
|
| 53 | }
|
| 54 | }
|
| 55 |
|