Skip to content

Commit adece8f

Browse files
committed
Extend sampler parameters
1 parent bb3e218 commit adece8f

2 files changed

Lines changed: 217 additions & 93 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package net.vulkanmod.vulkan.texture;
2+
3+
import static org.lwjgl.vulkan.VK10.*;
4+
import static org.lwjgl.vulkan.VK10.VK_COMPARE_OP_ALWAYS;
5+
import static org.lwjgl.vulkan.VK10.VK_FILTER_LINEAR;
6+
import static org.lwjgl.vulkan.VK10.VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
7+
import static org.lwjgl.vulkan.VK10.VK_SAMPLER_MIPMAP_MODE_LINEAR;
8+
9+
import static net.vulkanmod.vulkan.texture.SamplerManager.*;
10+
11+
public class SamplerInfo {
12+
final int encodedState;
13+
final int maxLod;
14+
final int maxAnisotropy;
15+
16+
public SamplerInfo() {
17+
this(VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT,
18+
VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST,
19+
0, false, 0, -1);
20+
}
21+
22+
public SamplerInfo(int addressModeU, int addressModeV,
23+
int minFilter, int magFilter, int mipmapMode,
24+
float maxLod, boolean anisotropy, float maxAnisotropy,
25+
int reductionMode)
26+
{
27+
this(addressModeU, addressModeV, minFilter, magFilter, mipmapMode, maxLod,
28+
anisotropy, maxAnisotropy, false, 0, reductionMode);
29+
}
30+
31+
public SamplerInfo(int addressModeU, int addressModeV,
32+
int minFilter, int magFilter, int mipmapMode,
33+
float maxLod, boolean anisotropy, float maxAnisotropy,
34+
boolean compare, int compareOp,
35+
int reductionMode)
36+
{
37+
this.maxLod = (int) maxLod;
38+
this.maxAnisotropy = (int) maxAnisotropy;
39+
40+
this.encodedState = getEncodedState(addressModeU, addressModeV, minFilter, magFilter, mipmapMode,
41+
anisotropy, compare, compareOp, reductionMode);
42+
}
43+
44+
public int getAddressModeU() {
45+
return (this.encodedState >> ADDRESS_MODE_U_OFFSET) & ADDRESS_MODE_BITS;
46+
}
47+
48+
public int getAddressModeV() {
49+
return (this.encodedState >> ADDRESS_MODE_V_OFFSET) & ADDRESS_MODE_BITS;
50+
}
51+
52+
public int getMinFilter() {
53+
return (this.encodedState >> MIN_FILTER_OFFSET) & 1;
54+
}
55+
56+
public int getMagFilter() {
57+
return (this.encodedState >> MAG_FILTER_OFFSET) & 1;
58+
}
59+
60+
public int getMipmapMode() {
61+
return (this.encodedState >> MIPMAP_MODE_OFFSET) & 1;
62+
}
63+
64+
public boolean getAnisotropy() {
65+
return ((this.encodedState >> ANISOTROPY_OFFSET) & 1) != 0;
66+
}
67+
68+
public boolean compareEnabled() {
69+
return ((this.encodedState >> COMPARE_ENABLED_OFFSET) & 1) != 0;
70+
}
71+
72+
public int getCompareOp() {
73+
return (this.encodedState >> COMPARE_ENABLED_OFFSET) & COMPARE_OP_BITS;
74+
}
75+
76+
public boolean hasReductionMode() {
77+
return ((this.encodedState >> REDUCTION_MODE_ENABLE_OFFSET) & 1) != 0;
78+
}
79+
80+
public int getReductionMode() {
81+
return (this.encodedState >> REDUCTION_MODE_OFFSET) & REDUCTION_MODE_BITS;
82+
}
83+
84+
public int getMaxAnisotropy() {
85+
return maxAnisotropy;
86+
}
87+
88+
public int getMaxLod() {
89+
return maxLod;
90+
}
91+
92+
@Override
93+
public boolean equals(Object o) {
94+
if (o == null || getClass() != o.getClass()) return false;
95+
96+
SamplerInfo samplerInfo = (SamplerInfo) o;
97+
return maxLod == samplerInfo.maxLod && maxAnisotropy == samplerInfo.maxAnisotropy && encodedState == samplerInfo.encodedState;
98+
}
99+
100+
@Override
101+
public int hashCode() {
102+
int result = encodedState;
103+
result = 31 * result + maxLod;
104+
result = 31 * result + maxAnisotropy;
105+
return result;
106+
}
107+
108+
public static Builder builder() {
109+
return new Builder();
110+
}
111+
112+
public static class Builder {
113+
int addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
114+
int addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
115+
116+
int minFilter = VK_FILTER_LINEAR;
117+
int magFilter = VK_FILTER_LINEAR;
118+
int mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
119+
120+
float maxLod = 0.0f;
121+
boolean anisotropy = false;
122+
float maxAnisotropy = 0.0f;
123+
124+
boolean compareEnabled = false;
125+
int compareOp = VK_COMPARE_OP_ALWAYS;
126+
127+
int reductionMode = 0;
128+
129+
Builder() {}
130+
131+
public Builder setAddressMode(int addressMode) {
132+
this.addressModeU = addressMode;
133+
this.addressModeV = addressMode;
134+
135+
return this;
136+
}
137+
138+
public Builder setFiltering(int minFilter, int magFilter, int mipmapMode) {
139+
this.minFilter = minFilter;
140+
this.magFilter = magFilter;
141+
this.mipmapMode = mipmapMode;
142+
143+
return this;
144+
}
145+
146+
public Builder setMaxLod(float maxLod) {
147+
this.maxLod = maxLod;
148+
149+
return this;
150+
}
151+
152+
public Builder setAnisotropy(float maxAnisotropy) {
153+
this.anisotropy = true;
154+
this.maxAnisotropy = maxAnisotropy;
155+
156+
return this;
157+
}
158+
159+
public Builder setCompare(boolean enable, int compareOp) {
160+
this.compareEnabled = enable;
161+
this.compareOp = compareOp;
162+
163+
return this;
164+
}
165+
166+
public Builder setReductionMode(int reductionMode) {
167+
this.reductionMode = reductionMode;
168+
169+
return this;
170+
}
171+
172+
public SamplerInfo createSamplerInfo() {
173+
return new SamplerInfo(addressModeU, addressModeV,
174+
minFilter, magFilter,
175+
mipmapMode, maxLod,
176+
anisotropy, maxAnisotropy,
177+
compareEnabled, compareOp,
178+
reductionMode);
179+
}
180+
}
181+
}

src/main/java/net/vulkanmod/vulkan/texture/SamplerManager.java

Lines changed: 36 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
public abstract class SamplerManager {
1717
public static final int ADDRESS_MODE_BITS = 2;
1818
public static final int REDUCTION_MODE_BITS = 2;
19+
public static final int COMPARE_OP_BITS = 3;
1920

2021
public static final int ADDRESS_MODE_U_OFFSET = 0;
2122
public static final int ADDRESS_MODE_V_OFFSET = 2;
2223
public static final int MIN_FILTER_OFFSET = 4;
2324
public static final int MAG_FILTER_OFFSET = 5;
2425
public static final int MIPMAP_MODE_OFFSET = 6;
2526
public static final int ANISOTROPY_OFFSET = 7;
26-
public static final int REDUCTION_MODE_ENABLE_OFFSET = 8;
27-
public static final int REDUCTION_MODE_OFFSET = 9;
27+
public static final int COMPARE_ENABLED_OFFSET = 8;
28+
public static final int COMPARE_OP_OFFSET = 9;
29+
public static final int REDUCTION_MODE_ENABLE_OFFSET = 12;
30+
public static final int REDUCTION_MODE_OFFSET = 13;
2831

2932
static final float MIP_BIAS = -0.5f;
3033

@@ -44,9 +47,16 @@ public static long getSampler(boolean clamp, boolean linearFiltering, int maxLod
4447

4548
public static long getSampler(int addressModeU, int addressModeV,
4649
int minFilter, int magFilter, int mipmapMode, float maxLod,
47-
boolean anisotropy, float maxAnisotropy, int reductionMode) {
48-
SamplerInfo samplerInfo = new SamplerInfo(addressModeU, addressModeV, minFilter, magFilter, mipmapMode, maxLod, anisotropy, maxAnisotropy, reductionMode);
50+
boolean anisotropy, float maxAnisotropy, int reductionMode)
51+
{
52+
SamplerInfo samplerInfo = new SamplerInfo(addressModeU, addressModeV,
53+
minFilter, magFilter, mipmapMode, maxLod,
54+
anisotropy, maxAnisotropy, reductionMode);
4955

56+
return getSampler(samplerInfo);
57+
}
58+
59+
public static long getSampler(SamplerInfo samplerInfo) {
5060
long sampler = SAMPLERS.getOrDefault(samplerInfo, 0L);
5161

5262
if (sampler == 0L) {
@@ -61,7 +71,7 @@ public static long getDefaultSampler() {
6171
return getSampler(false, false, 0);
6272
}
6373

64-
private static long createTextureSampler(SamplerInfo sampler) {
74+
public static long createTextureSampler(SamplerInfo sampler) {
6575
int state = sampler.encodedState;
6676

6777
try (MemoryStack stack = stackPush()) {
@@ -79,13 +89,13 @@ private static long createTextureSampler(SamplerInfo sampler) {
7989
samplerInfo.maxAnisotropy(sampler.getMaxAnisotropy());
8090
samplerInfo.borderColor(VK_BORDER_COLOR_INT_OPAQUE_WHITE);
8191
samplerInfo.unnormalizedCoordinates(false);
82-
samplerInfo.compareEnable(false);
83-
samplerInfo.compareOp(VK_COMPARE_OP_ALWAYS);
92+
samplerInfo.compareEnable(sampler.compareEnabled());
93+
samplerInfo.compareOp(sampler.getCompareOp());
8494

8595
samplerInfo.mipmapMode(sampler.getMipmapMode());
8696
samplerInfo.maxLod(sampler.getMaxLod());
8797
samplerInfo.minLod(0.0F);
88-
samplerInfo.mipLodBias(MIP_BIAS);
98+
samplerInfo.mipLodBias(0.0F);
8999

90100
// Reduction Mode
91101
if (sampler.hasReductionMode()) {
@@ -111,91 +121,24 @@ public static void cleanUp() {
111121
}
112122
}
113123

114-
public static class SamplerInfo {
115-
final int encodedState;
116-
final int maxLod;
117-
final int maxAnisotropy;
118-
119-
public SamplerInfo() {
120-
this(VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT,
121-
VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST,
122-
0, false, 0, -1);
123-
}
124-
125-
126-
127-
public SamplerInfo(int addressModeU, int addressModeV, int minFilter, int magFilter, int mipmapMode,
128-
float maxLod, boolean anisotropy, float maxAnisotropy, int reductionMode) {
129-
this.maxLod = (int) maxLod;
130-
this.maxAnisotropy = (int) maxAnisotropy;
131-
132-
int encodedState = (addressModeU & ADDRESS_MODE_BITS) << ADDRESS_MODE_U_OFFSET;
133-
encodedState |= (addressModeV & ADDRESS_MODE_BITS) << ADDRESS_MODE_V_OFFSET;
134-
encodedState |= (minFilter & 1) << MIN_FILTER_OFFSET;
135-
encodedState |= (magFilter & 1) << MAG_FILTER_OFFSET;
136-
encodedState |= (mipmapMode & 1) << MIPMAP_MODE_OFFSET;
137-
encodedState |= ((anisotropy ? 1 : 0) & 1) << ANISOTROPY_OFFSET;
138-
encodedState |= (reductionMode != -1 ? 1 : 0) << REDUCTION_MODE_ENABLE_OFFSET;
139-
encodedState |= (reductionMode & REDUCTION_MODE_BITS) << REDUCTION_MODE_OFFSET;
140-
141-
this.encodedState = encodedState;
142-
}
143-
144-
public int getAddressModeU() {
145-
return (this.encodedState >> ADDRESS_MODE_U_OFFSET) & ADDRESS_MODE_BITS;
146-
}
147-
148-
public int getAddressModeV() {
149-
return (this.encodedState >> ADDRESS_MODE_V_OFFSET) & ADDRESS_MODE_BITS;
150-
}
151-
152-
public int getMinFilter() {
153-
return (this.encodedState >> MIN_FILTER_OFFSET) & 1;
154-
}
155-
156-
public int getMagFilter() {
157-
return (this.encodedState >> MAG_FILTER_OFFSET) & 1;
158-
}
159-
160-
public int getMipmapMode() {
161-
return (this.encodedState >> MIPMAP_MODE_OFFSET) & 1;
162-
}
163-
164-
public boolean getAnisotropy() {
165-
return ((this.encodedState >> ANISOTROPY_OFFSET) & 1) != 0;
166-
}
167-
168-
public boolean hasReductionMode() {
169-
return ((this.encodedState >> REDUCTION_MODE_ENABLE_OFFSET) & 1) != 0;
170-
}
171-
172-
public int getReductionMode() {
173-
return (this.encodedState >> REDUCTION_MODE_OFFSET) & REDUCTION_MODE_BITS;
174-
}
175-
176-
public int getMaxAnisotropy() {
177-
return maxAnisotropy;
178-
}
179-
180-
public int getMaxLod() {
181-
return maxLod;
182-
}
183-
184-
@Override
185-
public boolean equals(Object o) {
186-
if (o == null || getClass() != o.getClass()) return false;
187-
188-
SamplerInfo samplerInfo = (SamplerInfo) o;
189-
return maxLod == samplerInfo.maxLod && maxAnisotropy == samplerInfo.maxAnisotropy && encodedState == samplerInfo.encodedState;
190-
}
191-
192-
@Override
193-
public int hashCode() {
194-
int result = encodedState;
195-
result = 31 * result + maxLod;
196-
result = 31 * result + maxAnisotropy;
197-
return result;
198-
}
124+
static int getEncodedState(int addressModeU, int addressModeV,
125+
int minFilter, int magFilter, int mipmapMode,
126+
boolean anisotropy,
127+
boolean compare, int compareOp,
128+
int reductionMode)
129+
{
130+
int encodedState = (addressModeU & ADDRESS_MODE_BITS) << ADDRESS_MODE_U_OFFSET;
131+
encodedState |= (addressModeV & ADDRESS_MODE_BITS) << ADDRESS_MODE_V_OFFSET;
132+
encodedState |= (minFilter & 1) << MIN_FILTER_OFFSET;
133+
encodedState |= (magFilter & 1) << MAG_FILTER_OFFSET;
134+
encodedState |= (mipmapMode & 1) << MIPMAP_MODE_OFFSET;
135+
encodedState |= ((anisotropy ? 1 : 0) & 1) << ANISOTROPY_OFFSET;
136+
encodedState |= ((compare ? 1 : 0) & 1) << COMPARE_ENABLED_OFFSET;
137+
encodedState |= (compareOp & COMPARE_OP_BITS) << COMPARE_OP_OFFSET;
138+
encodedState |= (reductionMode != -1 ? 1 : 0) << REDUCTION_MODE_ENABLE_OFFSET;
139+
encodedState |= (reductionMode & REDUCTION_MODE_BITS) << REDUCTION_MODE_OFFSET;
140+
141+
return encodedState;
199142
}
200143

201144
}

0 commit comments

Comments
 (0)