Skip to content

Commit

Permalink
Use immutable samplers
Browse files Browse the repository at this point in the history
  • Loading branch information
thr3343 committed Oct 6, 2024
1 parent 4d5f6ac commit 59ec81f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/vulkanmod/vulkan/shader/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.vulkanmod.vulkan.shader.descriptor.UBO;
import net.vulkanmod.vulkan.shader.layout.AlignedStruct;
import net.vulkanmod.vulkan.shader.layout.PushConstants;
import net.vulkanmod.vulkan.texture.SamplerManager;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.texture.VulkanImage;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -100,12 +101,13 @@ protected void createDescriptorSetLayout() {
uboLayoutBinding.stageFlags(ubo.getStages());
}

final LongBuffer immutableSampler = stack.longs(SamplerManager.getTextureSampler((byte) 0)); //vertex stage samplers always use nearest Sampling
for (ImageDescriptor imageDescriptor : this.imageDescriptors) {
VkDescriptorSetLayoutBinding samplerLayoutBinding = bindings.get(imageDescriptor.getBinding());
samplerLayoutBinding.binding(imageDescriptor.getBinding());
samplerLayoutBinding.descriptorCount(1);
samplerLayoutBinding.descriptorType(imageDescriptor.getType());
samplerLayoutBinding.pImmutableSamplers(null);
samplerLayoutBinding.pImmutableSamplers(imageDescriptor.getStages()==VK_SHADER_STAGE_VERTEX_BIT ? immutableSampler : null);
samplerLayoutBinding.stageFlags(imageDescriptor.getStages());
}

Expand Down Expand Up @@ -670,7 +672,7 @@ public static int getStageFromString(String s) {
return switch (s) {
case "vertex" -> VK_SHADER_STAGE_VERTEX_BIT;
case "fragment" -> VK_SHADER_STAGE_FRAGMENT_BIT;
case "all" -> VK_SHADER_STAGE_ALL_GRAPHICS;
case "all" -> VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; //VK_SHADER_STAGE_ALL_GRAPHICS includes tessellation/geometry stages, which are unused
case "compute" -> VK_SHADER_STAGE_COMPUTE_BIT;

default -> throw new RuntimeException("cannot identify type..");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.vulkanmod.vulkan.shader.descriptor;

import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.texture.VTextureSelector;
import net.vulkanmod.vulkan.texture.VulkanImage;

Expand All @@ -14,6 +15,7 @@ public class ImageDescriptor implements Descriptor {
public final int imageIdx;

public final boolean isStorageImage;
private final int stage;
public boolean useSampler;
public boolean isReadOnlyLayout;
private int layout;
Expand All @@ -24,12 +26,19 @@ public ImageDescriptor(int binding, String type, String name, int imageIdx) {
}

public ImageDescriptor(int binding, String type, String name, int imageIdx, boolean isStorageImage) {
final int stage1 = switch (name) {
case "Sampler0", "DiffuseSampler", "SamplerProj" -> VK_SHADER_STAGE_FRAGMENT_BIT;
case "Sampler1", "Sampler2" -> VK_SHADER_STAGE_VERTEX_BIT;
default -> VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
};

this.binding = binding;
this.qualifier = type;
this.name = name;
this.isStorageImage = isStorageImage;
this.useSampler = !isStorageImage;
this.imageIdx = imageIdx;
this.stage = stage1;

descriptorType = isStorageImage ? VK_DESCRIPTOR_TYPE_STORAGE_IMAGE : VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
setLayout(isStorageImage ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Expand All @@ -47,7 +56,7 @@ public int getType() {

@Override
public int getStages() {
return VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT;
return stage;
}

public void setLayout(int layout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void uploadSubTexture(int mipLevel, int width, int height, int xOf
public static int getTextureIdx(String name) {
return switch (name) {
case "Sampler0", "DiffuseSampler" -> 0;
case "Sampler1" -> 1;
case "Sampler1", "SamplerProj" -> 1;
case "Sampler2" -> 2;
case "Sampler3" -> 3;
case "Sampler4" -> 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"attributes": [],
"samplers": [
{ "name": "Sampler0" },
{ "name": "Sampler1" }
{ "name": "SamplerProj" }
],
"uniforms": [
{ "name": "GameTime", "type": "float", "count": 1, "values": [ 0.0 ] },
Expand Down

0 comments on commit 59ec81f

Please sign in to comment.