minecraft-src/com/mojang/blaze3d/opengl/GlTexture.java
2025-07-04 03:45:38 +03:00

94 lines
2.7 KiB
Java

package com.mojang.blaze3d.opengl;
import com.mojang.blaze3d.textures.AddressMode;
import com.mojang.blaze3d.textures.FilterMode;
import com.mojang.blaze3d.textures.GpuTexture;
import com.mojang.blaze3d.textures.TextureFormat;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntIterator;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class GlTexture extends GpuTexture {
protected final int id;
private final Int2IntMap fboCache = new Int2IntOpenHashMap();
protected boolean closed;
protected boolean modesDirty = true;
protected GlTexture(String label, TextureFormat format, int width, int height, int mipLevels, int id) {
super(label, format, width, height, mipLevels);
this.id = id;
}
@Override
public void close() {
if (!this.closed) {
this.closed = true;
GlStateManager._deleteTexture(this.id);
IntIterator var1 = this.fboCache.values().iterator();
while (var1.hasNext()) {
int i = (Integer)var1.next();
GlStateManager._glDeleteFramebuffers(i);
}
}
}
@Override
public boolean isClosed() {
return this.closed;
}
public int getFbo(DirectStateAccess directStateAccess, @Nullable GpuTexture texture) {
int i = texture == null ? 0 : ((GlTexture)texture).id;
return this.fboCache.computeIfAbsent(i, (Int2IntFunction)(j -> {
int k = directStateAccess.createFrameBufferObject();
directStateAccess.bindFrameBufferTextures(k, this.id, i, 0, 0);
return k;
}));
}
public void flushModeChanges() {
if (this.modesDirty) {
GlStateManager._texParameter(3553, 10242, GlConst.toGl(this.addressModeU));
GlStateManager._texParameter(3553, 10243, GlConst.toGl(this.addressModeV));
switch (this.minFilter) {
case NEAREST:
GlStateManager._texParameter(3553, 10241, this.useMipmaps ? 9986 : 9728);
break;
case LINEAR:
GlStateManager._texParameter(3553, 10241, this.useMipmaps ? 9987 : 9729);
}
switch (this.magFilter) {
case NEAREST:
GlStateManager._texParameter(3553, 10240, 9728);
break;
case LINEAR:
GlStateManager._texParameter(3553, 10240, 9729);
}
this.modesDirty = false;
}
}
public int glId() {
return this.id;
}
@Override
public void setAddressMode(AddressMode addressMode, AddressMode addressMode2) {
super.setAddressMode(addressMode, addressMode2);
this.modesDirty = true;
}
@Override
public void setTextureFilter(FilterMode filterMode, FilterMode filterMode2, boolean bl) {
super.setTextureFilter(filterMode, filterMode2, bl);
this.modesDirty = true;
}
}