1616public 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