| 1 | package com.mojang.renderpearl.frontend;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 4 | import com.mojang.renderpearl.api.device.GpuSurface;
|
| 5 | import com.mojang.renderpearl.api.device.SurfaceException;
|
| 6 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 7 | import com.mojang.renderpearl.backend.api.GpuSurfaceBackend;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.Optional;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class FrontendGpuSurface implements GpuSurface {
|
| 15 | private final GpuSurfaceBackend backend;
|
| 16 | private boolean hasImageAcquired = false;
|
| 17 | private boolean hasBlittedTexture = false;
|
| 18 | private Optional<GpuSurface.Configuration> currentConfiguration = Optional.empty();
|
| 19 |
|
| 20 | public FrontendGpuSurface(final GpuSurfaceBackend backend) {
|
| 21 | this.backend = backend;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public void close() {
|
| 26 | if (this.hasImageAcquired) {
|
| 27 | throw new IllegalStateException("Cannot close a surface while it is acquired");
|
| 28 | }
|
| 29 |
|
| 30 | this.backend.close();
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void configure(final GpuSurface.Configuration config) throws SurfaceException {
|
| 35 | if (this.hasImageAcquired) {
|
| 36 | throw new IllegalStateException("Cannot configure a surface while it is acquired");
|
| 37 | }
|
| 38 |
|
| 39 | if (!this.supportedPresentModes().contains(config.presentMode())) {
|
| 40 | throw new SurfaceException("Surface does not support present mode " + config.presentMode() + " (supported: " + this.supportedPresentModes() + ")");
|
| 41 | }
|
| 42 |
|
| 43 | this.backend.configure(config);
|
| 44 | this.currentConfiguration = Optional.of(config);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public Optional<GpuSurface.Configuration> currentConfiguration() {
|
| 49 | return this.currentConfiguration;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public Collection<GpuSurface.PresentMode> supportedPresentModes() {
|
| 54 | return this.backend.supportedPresentModes();
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public boolean isSuboptimal() {
|
| 59 | return this.backend.isSuboptimal();
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public boolean isAcquired() {
|
| 64 | return this.hasImageAcquired;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void acquireNextTexture() throws SurfaceException {
|
| 69 | if (this.hasImageAcquired) {
|
| 70 | throw new IllegalStateException("Cannot acquire a surface while it is already acquired");
|
| 71 | }
|
| 72 |
|
| 73 | if (this.currentConfiguration.isEmpty()) {
|
| 74 | throw new IllegalStateException("Cannot acquire an unconfigured surface");
|
| 75 | }
|
| 76 |
|
| 77 | this.backend.acquireNextTexture();
|
| 78 | this.hasImageAcquired = true;
|
| 79 | this.hasBlittedTexture = false;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public void blitFromTexture(final CommandEncoder commandEncoder, final GpuTextureView textureView) {
|
| 84 | if (commandEncoder instanceof FrontendCommandEncoder frontendCommandEncoder) {
|
| 85 | if (frontendCommandEncoder.isInRenderPass()) {
|
| 86 | throw new IllegalStateException("Close the existing render pass before presenting with a command encoder");
|
| 87 | }
|
| 88 |
|
| 89 | if (!textureView.texture().getFormat().hasColorAspect()) {
|
| 90 | throw new IllegalStateException("Cannot present a non-color texture!");
|
| 91 | }
|
| 92 |
|
| 93 | if ((textureView.texture().usage() & 2) == 0) {
|
| 94 | throw new IllegalStateException("Color texture must have USAGE_COPY_SRC to presented to the screen");
|
| 95 | }
|
| 96 |
|
| 97 | if (textureView.texture().getDepthOrLayers() > 1) {
|
| 98 | throw new UnsupportedOperationException("Textures with multiple depths or layers are not yet supported for presentation");
|
| 99 | }
|
| 100 |
|
| 101 | if (!this.hasImageAcquired) {
|
| 102 | throw new IllegalStateException("Cannot present to an unacquired surface");
|
| 103 | }
|
| 104 |
|
| 105 | if (this.hasBlittedTexture) {
|
| 106 | throw new IllegalStateException("Already blitted to this frame!");
|
| 107 | }
|
| 108 |
|
| 109 | this.backend.blitFromTexture(frontendCommandEncoder.backend(), textureView);
|
| 110 | this.hasBlittedTexture = true;
|
| 111 | } else {
|
| 112 | throw new IllegalArgumentException("CommandEncoder must be instance of FrontendCommandEncoder");
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void present() {
|
| 118 | if (!this.hasImageAcquired) {
|
| 119 | throw new IllegalStateException("Cannot present to a surface if it isn't acquired");
|
| 120 | }
|
| 121 |
|
| 122 | if (!this.hasBlittedTexture) {
|
| 123 | throw new IllegalStateException("Must blit to surface before presenting!");
|
| 124 | }
|
| 125 |
|
| 126 | this.backend.present();
|
| 127 | this.hasImageAcquired = false;
|
| 128 | }
|
| 129 | }
|
| 130 |
|