| 1 | package com.mojang.renderpearl.frontend;
|
| 2 |
|
| 3 | import com.mojang.jtracy.TracyClient;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 7 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 8 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 9 | import com.mojang.renderpearl.api.device.DeviceInfo;
|
| 10 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 11 | import com.mojang.renderpearl.api.device.GpuSurface;
|
| 12 | import com.mojang.renderpearl.api.pipeline.CompiledRenderPipeline;
|
| 13 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 14 | import com.mojang.renderpearl.api.pipeline.ShaderSource;
|
| 15 | import com.mojang.renderpearl.api.textures.AddressMode;
|
| 16 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 17 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 18 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 19 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 20 | import com.mojang.renderpearl.backend.api.GpuDeviceBackend;
|
| 21 | import com.mojang.renderpearl.frontend.shaders.PipelineBuilder;
|
| 22 | import java.nio.ByteBuffer;
|
| 23 | import java.util.List;
|
| 24 | import java.util.OptionalDouble;
|
| 25 | import java.util.concurrent.CompletableFuture;
|
| 26 | import java.util.concurrent.Executor;
|
| 27 | import java.util.function.BooleanSupplier;
|
| 28 | import java.util.function.Supplier;
|
| 29 | import net.fabricmc.api.EnvType;
|
| 30 | import net.fabricmc.api.Environment;
|
| 31 | import net.minecraft.SharedConstants;
|
| 32 | import net.minecraft.util.Mth;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class FrontendGpuDevice implements GpuDevice {
|
| 38 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 39 | public static final boolean STRICT_VALIDATION = SharedConstants.IS_RUNNING_IN_IDE;
|
| 40 | private final GpuDeviceBackend backend;
|
| 41 | private final @Nullable TracyGpuProfiler profiler;
|
| 42 | private final CommandEncoder encoder;
|
| 43 | private final PipelineBuilder pipelineBuilder;
|
| 44 |
|
| 45 | public FrontendGpuDevice(final GpuDeviceBackend backend) {
|
| 46 | this.backend = backend;
|
| 47 | if (TracyClient.isAvailable()) {
|
| 48 | this.profiler = new TracyGpuProfiler(this);
|
| 49 | } else {
|
| 50 | this.profiler = null;
|
| 51 | }
|
| 52 |
|
| 53 | this.encoder = new FrontendCommandEncoder(this.profiler, backend, backend.createCommandEncoder());
|
| 54 | this.pipelineBuilder = new PipelineBuilder(backend);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public GpuSurface createSurface(final long windowHandle, final BooleanSupplier isIconified) {
|
| 59 | return new FrontendGpuSurface(this.backend.createSurface(windowHandle, isIconified));
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public CommandEncoder createCommandEncoder() {
|
| 64 | return this.encoder;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public GpuSampler createSampler(
|
| 69 | final AddressMode addressModeU,
|
| 70 | final AddressMode addressModeV,
|
| 71 | final FilterMode minFilter,
|
| 72 | final FilterMode magFilter,
|
| 73 | final int maxAnisotropy,
|
| 74 | final OptionalDouble maxLod
|
| 75 | ) {
|
| 76 | int maxSupportedAnisotropy = this.getDeviceInfo().limits().maxAnisotropy();
|
| 77 | if (maxAnisotropy >= 1 && maxAnisotropy <= maxSupportedAnisotropy) {
|
| 78 | return this.backend.createSampler(addressModeU, addressModeV, minFilter, magFilter, maxAnisotropy, maxLod);
|
| 79 | } else {
|
| 80 | throw new IllegalArgumentException("maxAnisotropy out of range; must be >= 1 and <= " + maxSupportedAnisotropy + ", but was " + maxAnisotropy);
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public GpuTexture createTexture(
|
| 86 | final @Nullable Supplier<String> label,
|
| 87 | final @GpuTexture.Usage int usage,
|
| 88 | final GpuFormat format,
|
| 89 | final int width,
|
| 90 | final int height,
|
| 91 | final int depthOrLayers,
|
| 92 | final int mipLevels
|
| 93 | ) {
|
| 94 | return this.createTexture(this.isDebuggingEnabled() && label != null ? label.get() : null, usage, format, width, height, depthOrLayers, mipLevels);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public GpuTexture createTexture(
|
| 99 | final @Nullable String label,
|
| 100 | final @GpuTexture.Usage int usage,
|
| 101 | final GpuFormat format,
|
| 102 | final int width,
|
| 103 | final int height,
|
| 104 | final int depthOrLayers,
|
| 105 | final int mipLevels
|
| 106 | ) {
|
| 107 | this.verifyTextureCreationArgs(usage, width, height, depthOrLayers, mipLevels);
|
| 108 | return this.backend.createTexture(label, usage, format, width, height, depthOrLayers, mipLevels);
|
| 109 | }
|
| 110 |
|
| 111 | private void verifyTextureCreationArgs(final @GpuTexture.Usage int usage, final int width, final int height, final int depthOrLayers, final int mipLevels) {
|
| 112 | if (mipLevels < 1) {
|
| 113 | throw new IllegalArgumentException("mipLevels must be at least 1");
|
| 114 | }
|
| 115 |
|
| 116 | int maxDimension = Math.max(width, height);
|
| 117 | int maxMipSupported = Mth.log2(maxDimension) + 1;
|
| 118 | if (mipLevels > maxMipSupported) {
|
| 119 | throw new IllegalArgumentException(
|
| 120 | "mipLevels must be at most " + maxMipSupported + " for a texture of width " + width + " and height " + height + " (asked for " + mipLevels + " mipLevels)"
|
| 121 | );
|
| 122 | }
|
| 123 |
|
| 124 | if (depthOrLayers < 1) {
|
| 125 | throw new IllegalArgumentException("depthOrLayers must be at least 1");
|
| 126 | }
|
| 127 |
|
| 128 | boolean isCubemap = (usage & 16) != 0;
|
| 129 | if (isCubemap) {
|
| 130 | if (width != height) {
|
| 131 | throw new IllegalArgumentException("Cubemap compatible textures must be square, but size is " + width + "x" + height);
|
| 132 | }
|
| 133 |
|
| 134 | if (depthOrLayers % 6 != 0) {
|
| 135 | throw new IllegalArgumentException("Cubemap compatible textures must have a layer count with a multiple of 6, was " + depthOrLayers);
|
| 136 | }
|
| 137 |
|
| 138 | if (depthOrLayers > 6) {
|
| 139 | throw new UnsupportedOperationException("Array textures are not yet supported");
|
| 140 | }
|
| 141 | } else if (depthOrLayers > 1) {
|
| 142 | throw new UnsupportedOperationException("Array or 3D textures are not yet supported");
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public GpuTextureView createTextureView(final GpuTexture texture) {
|
| 148 | return this.createTextureView(texture, 0, texture.getMipLevels());
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public GpuTextureView createTextureView(final GpuTexture texture, final int baseMipLevel, final int mipLevels) {
|
| 153 | this.verifyTextureViewCreationArgs(texture, baseMipLevel, mipLevels);
|
| 154 | return this.backend.createTextureView(texture, baseMipLevel, mipLevels);
|
| 155 | }
|
| 156 |
|
| 157 | private void verifyTextureViewCreationArgs(final GpuTexture texture, final int baseMipLevel, final int mipLevels) {
|
| 158 | if (texture.isClosed()) {
|
| 159 | throw new IllegalArgumentException("Can't create texture view with closed texture");
|
| 160 | } else if (baseMipLevel < 0 || baseMipLevel + mipLevels > texture.getMipLevels()) {
|
| 161 | throw new IllegalArgumentException(
|
| 162 | mipLevels + " mip levels starting from " + baseMipLevel + " would be out of range for texture with only " + texture.getMipLevels() + " mip levels"
|
| 163 | );
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public GpuBuffer createBuffer(final @Nullable Supplier<String> label, final @GpuBuffer.Usage int usage, final long size) {
|
| 169 | if (size <= 0L) {
|
| 170 | throw new IllegalArgumentException("Buffer size must be greater than zero");
|
| 171 | } else {
|
| 172 | return this.backend.createBuffer(label, usage, size);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public GpuBuffer createBuffer(final @Nullable Supplier<String> label, final @GpuBuffer.Usage int usage, final ByteBuffer data) {
|
| 178 | if (!data.hasRemaining()) {
|
| 179 | throw new IllegalArgumentException("Buffer source must not be empty");
|
| 180 | } else {
|
| 181 | return this.backend.createBuffer(label, usage, data);
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public List<String> getLastDebugMessages() {
|
| 187 | return this.backend.getLastDebugMessages();
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public boolean isDebuggingEnabled() {
|
| 192 | return this.backend.isDebuggingEnabled();
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public CompletableFuture<CompiledRenderPipeline.Pending> compilePipeline(
|
| 197 | final RenderPipeline pipeline, final ShaderSource shaderSource, final Executor executor
|
| 198 | ) {
|
| 199 | return this.pipelineBuilder.compilePipeline(pipeline, shaderSource, executor);
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public void close() {
|
| 204 | this.pipelineBuilder.close();
|
| 205 | this.backend.close();
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public GpuQueryPool createTimestampQueryPool(final int size) {
|
| 210 | return this.backend.createTimestampQueryPool(size);
|
| 211 | }
|
| 212 |
|
| 213 | long getTimestampCalibrationOffset() {
|
| 214 | return this.backend.getTimestampCalibrationOffset();
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public DeviceInfo getDeviceInfo() {
|
| 219 | return this.backend.getDeviceInfo();
|
| 220 | }
|
| 221 | }
|
| 222 |
|