| 1 | package com.mojang.blaze3d.systems;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 5 | import com.mojang.renderpearl.api.textures.AddressMode;
|
| 6 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 7 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 8 | import java.util.OptionalDouble;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class SamplerCache {
|
| 14 | private final GpuSampler[] samplers = new GpuSampler[32];
|
| 15 |
|
| 16 | public void initialize() {
|
| 17 | GpuDevice device = RenderSystem.getDevice();
|
| 18 | if (AddressMode.values().length == 2 && FilterMode.values().length == 2) {
|
| 19 | for (AddressMode addressModeU : AddressMode.values()) {
|
| 20 | for (AddressMode addressModeV : AddressMode.values()) {
|
| 21 | for (FilterMode minFilter : FilterMode.values()) {
|
| 22 | for (FilterMode magFilter : FilterMode.values()) {
|
| 23 | for (boolean useMipmaps : new boolean[]{true, false}) {
|
| 24 | this.samplers[encode(addressModeU, addressModeV, minFilter, magFilter, useMipmaps)] = device.createSampler(
|
| 25 | addressModeU, addressModeV, minFilter, magFilter, 1, useMipmaps ? OptionalDouble.empty() : OptionalDouble.of(0.0)
|
| 26 | );
|
| 27 | }
|
| 28 | }
|
| 29 | }
|
| 30 | }
|
| 31 | }
|
| 32 | } else {
|
| 33 | throw new IllegalStateException("AddressMode and FilterMode enum sizes must be 2 - if you expanded them, please update SamplerCache");
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public GpuSampler getSampler(
|
| 38 | final AddressMode addressModeU, final AddressMode addressModeV, final FilterMode minFilter, final FilterMode magFilter, final boolean useMipmaps
|
| 39 | ) {
|
| 40 | return this.samplers[encode(addressModeU, addressModeV, minFilter, magFilter, useMipmaps)];
|
| 41 | }
|
| 42 |
|
| 43 | public GpuSampler getClampToEdge(final FilterMode minMag) {
|
| 44 | return this.getSampler(AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, minMag, minMag, false);
|
| 45 | }
|
| 46 |
|
| 47 | public GpuSampler getClampToEdge(final FilterMode minMag, final boolean mipmaps) {
|
| 48 | return this.getSampler(AddressMode.CLAMP_TO_EDGE, AddressMode.CLAMP_TO_EDGE, minMag, minMag, mipmaps);
|
| 49 | }
|
| 50 |
|
| 51 | public GpuSampler getRepeat(final FilterMode minMag) {
|
| 52 | return this.getSampler(AddressMode.REPEAT, AddressMode.REPEAT, minMag, minMag, false);
|
| 53 | }
|
| 54 |
|
| 55 | public GpuSampler getRepeat(final FilterMode minMag, final boolean mipmaps) {
|
| 56 | return this.getSampler(AddressMode.REPEAT, AddressMode.REPEAT, minMag, minMag, mipmaps);
|
| 57 | }
|
| 58 |
|
| 59 | public void close() {
|
| 60 | for (GpuSampler sampler : this.samplers) {
|
| 61 | sampler.close();
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @VisibleForTesting
|
| 66 | static int encode(
|
| 67 | final AddressMode addressModeU, final AddressMode addressModeV, final FilterMode minFilter, final FilterMode magFilter, final boolean useMipmaps
|
| 68 | ) {
|
| 69 | int result = 0;
|
| 70 | result |= addressModeU.ordinal() & 1;
|
| 71 | result |= (addressModeV.ordinal() & 1) << 1;
|
| 72 | result |= (minFilter.ordinal() & 1) << 2;
|
| 73 | result |= (magFilter.ordinal() & 1) << 3;
|
| 74 | if (useMipmaps) {
|
| 75 | result |= 16;
|
| 76 | }
|
| 77 |
|
| 78 | return result;
|
| 79 | }
|
| 80 | }
|
| 81 |
|