Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Samplers] Patch: Simplify + Optimize Sampler Management #485

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/net/vulkanmod/gl/GlRenderbuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void updateSampler() {
default -> 0;
};

vulkanImage.updateTextureSampler(maxLod, samplerFlags);
vulkanImage.updateTextureSampler(samplerFlags);
}

private void uploadImage(ByteBuffer pixels) {
Expand Down
15 changes: 2 additions & 13 deletions src/main/java/net/vulkanmod/gl/GlTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static void texParameteri(int target, int pName, int param) {

switch (pName) {
case GL30.GL_TEXTURE_MAX_LEVEL -> boundTexture.setMaxLevel(param);
case GL30.GL_TEXTURE_MAX_LOD -> boundTexture.setMaxLod(param);
case GL30.GL_TEXTURE_MAX_LOD -> {}
case GL30.GL_TEXTURE_MIN_LOD -> {}
case GL30.GL_TEXTURE_LOD_BIAS -> {}

Expand Down Expand Up @@ -170,7 +170,6 @@ public static GlTexture getBoundTexture() {

boolean needsUpdate = false;
int maxLevel = 0;
int maxLod = 0;
int minFilter, magFilter = GL11.GL_LINEAR;

boolean clamp = true;
Expand Down Expand Up @@ -225,7 +224,7 @@ void updateSampler() {
default -> 0;
};

vulkanImage.updateTextureSampler(maxLod, samplerFlags);
vulkanImage.updateTextureSampler(samplerFlags);
}

private void uploadImage(ByteBuffer pixels) {
Expand Down Expand Up @@ -256,16 +255,6 @@ void setMaxLevel(int l) {
}
}

void setMaxLod(int l) {
if (l < 0)
throw new IllegalStateException("max level cannot be < 0.");

if (maxLod != l) {
maxLod = l;
updateSampler();
}
}

void setMagFilter(int v) {
switch (v) {
case GL11.GL_LINEAR, GL11.GL_NEAREST -> {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/vulkanmod/vulkan/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.vulkanmod.vulkan.shader.PipelineState;
import net.vulkanmod.vulkan.shader.Uniforms;
import net.vulkanmod.vulkan.shader.layout.PushConstants;
import net.vulkanmod.vulkan.texture.SamplerManager;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.util.VUtil;
import net.vulkanmod.vulkan.util.VkResult;
Expand Down Expand Up @@ -462,6 +463,8 @@ public void cleanUpResources() {

PipelineManager.destroyPipelines();
VTextureSelector.getWhiteTexture().free();
SamplerManager.cleanUp();

}

private void destroySyncObjects() {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/vulkanmod/vulkan/texture/SamplerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ public abstract class SamplerManager {

static final Short2LongMap SAMPLERS = new Short2LongOpenHashMap();

public static long getTextureSampler(byte maxLod, byte flags) {
short key = (short) (flags | (maxLod << 8));
long sampler = SAMPLERS.getOrDefault(key, 0L);
public static long getTextureSampler(byte flags) {
long sampler = SAMPLERS.getOrDefault(flags, 0L);

if (sampler == 0L) {
sampler = createTextureSampler(maxLod, flags);
SAMPLERS.put(key, sampler);
sampler = createTextureSampler(flags);
SAMPLERS.put(flags, sampler);
}

return sampler;
}

private static long createTextureSampler(byte maxLod, byte flags) {
private static long createTextureSampler(byte flags) {
Validate.isTrue(
(flags & (REDUCTION_MIN_BIT | REDUCTION_MAX_BIT)) != (REDUCTION_MIN_BIT | REDUCTION_MAX_BIT)
);
Expand Down Expand Up @@ -78,7 +77,7 @@ private static long createTextureSampler(byte maxLod, byte flags) {
} else {
samplerInfo.mipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST);
}
samplerInfo.maxLod(maxLod);
samplerInfo.maxLod(VK_LOD_CLAMP_NONE);
samplerInfo.minLod(0.0F);
samplerInfo.mipLodBias(MIP_BIAS);
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/net/vulkanmod/vulkan/texture/VulkanImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public VulkanImage(long id, int format, int mipLevels, int width, int height, in
this.usage = usage;
this.aspect = getAspect(this.format);

this.sampler = SamplerManager.getTextureSampler((byte) this.mipLevels, (byte) 0);
this.sampler = SamplerManager.getTextureSampler((byte) 0);
}

private VulkanImage(Builder builder) {
Expand All @@ -78,7 +78,7 @@ public static VulkanImage createTextureImage(Builder builder) {
image.createImage(builder.mipLevels, builder.width, builder.height, builder.format, builder.usage);
image.mainImageView = createImageView(image.id, builder.format, image.aspect, builder.mipLevels);

image.sampler = SamplerManager.getTextureSampler(builder.mipLevels, builder.samplerFlags);
image.sampler = SamplerManager.getTextureSampler(builder.samplerFlags);

if (builder.levelViews) {
image.levelImageViews = new long[builder.mipLevels];
Expand Down Expand Up @@ -242,11 +242,7 @@ public void updateTextureSampler(boolean blur, boolean clamp, boolean mipmaps) {
}

public void updateTextureSampler(byte flags) {
updateTextureSampler(this.mipLevels - 1, flags);
}

public void updateTextureSampler(int maxLod, byte flags) {
this.sampler = SamplerManager.getTextureSampler((byte) maxLod, flags);
this.sampler = SamplerManager.getTextureSampler(flags);
}

public void transitionImageLayout(MemoryStack stack, VkCommandBuffer commandBuffer, int newLayout) {
Expand Down
Loading