Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | |
| 23 | import android.content.res.Resources; |
| 24 | import android.os.Bundle; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
| 29 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 30 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 31 | * Sampler object which defines how data is extracted from textures. Samplers |
| 32 | * are attached to Program objects (currently only ProgramFragment) when those objects |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 33 | * need to access texture data. |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 34 | **/ |
| 35 | public class Sampler extends BaseObj { |
| 36 | public enum Value { |
| 37 | NEAREST (0), |
| 38 | LINEAR (1), |
| 39 | LINEAR_MIP_LINEAR (2), |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 40 | LINEAR_MIP_NEAREST (5), |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 41 | WRAP (3), |
| 42 | CLAMP (4); |
| 43 | |
| 44 | int mID; |
| 45 | Value(int id) { |
| 46 | mID = id; |
| 47 | } |
| 48 | } |
| 49 | |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 50 | Value mMin; |
| 51 | Value mMag; |
| 52 | Value mWrapS; |
| 53 | Value mWrapT; |
| 54 | Value mWrapR; |
| 55 | float mAniso; |
| 56 | |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 57 | Sampler(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 58 | super(id, rs); |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 61 | /** |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 62 | * @return minification setting for the sampler |
| 63 | */ |
| 64 | public Value getMinification() { |
| 65 | return mMin; |
| 66 | } |
| 67 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 68 | /** |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 69 | * @return magnification setting for the sampler |
| 70 | */ |
| 71 | public Value getMagnification() { |
| 72 | return mMag; |
| 73 | } |
| 74 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 75 | /** |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 76 | * @return S wrapping mode for the sampler |
| 77 | */ |
| 78 | public Value getWrapS() { |
| 79 | return mWrapS; |
| 80 | } |
| 81 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 82 | /** |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 83 | * @return T wrapping mode for the sampler |
| 84 | */ |
| 85 | public Value getWrapT() { |
| 86 | return mWrapT; |
| 87 | } |
| 88 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 89 | /** |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 90 | * @return anisotropy setting for the sampler |
| 91 | */ |
| 92 | public float getAnisotropy() { |
| 93 | return mAniso; |
| 94 | } |
| 95 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 96 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 97 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 98 | * clamp. |
| 99 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 100 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 101 | * |
| 102 | * @return Sampler |
| 103 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 104 | public static Sampler CLAMP_NEAREST(RenderScript rs) { |
| 105 | if(rs.mSampler_CLAMP_NEAREST == null) { |
| 106 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 107 | b.setMinification(Value.NEAREST); |
| 108 | b.setMagnification(Value.NEAREST); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 109 | b.setWrapS(Value.CLAMP); |
| 110 | b.setWrapT(Value.CLAMP); |
| 111 | rs.mSampler_CLAMP_NEAREST = b.create(); |
| 112 | } |
| 113 | return rs.mSampler_CLAMP_NEAREST; |
| 114 | } |
| 115 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 116 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 117 | * Retrieve a sampler with min and mag set to linear and wrap modes set to |
| 118 | * clamp. |
| 119 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 120 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 121 | * |
| 122 | * @return Sampler |
| 123 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 124 | public static Sampler CLAMP_LINEAR(RenderScript rs) { |
| 125 | if(rs.mSampler_CLAMP_LINEAR == null) { |
| 126 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 127 | b.setMinification(Value.LINEAR); |
| 128 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 129 | b.setWrapS(Value.CLAMP); |
| 130 | b.setWrapT(Value.CLAMP); |
| 131 | rs.mSampler_CLAMP_LINEAR = b.create(); |
| 132 | } |
| 133 | return rs.mSampler_CLAMP_LINEAR; |
| 134 | } |
| 135 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 136 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 137 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 138 | * to and wrap modes set to clamp. |
| 139 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 140 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 141 | * |
| 142 | * @return Sampler |
| 143 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 144 | public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { |
| 145 | if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { |
| 146 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 147 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 148 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 149 | b.setWrapS(Value.CLAMP); |
| 150 | b.setWrapT(Value.CLAMP); |
| 151 | rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); |
| 152 | } |
| 153 | return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; |
| 154 | } |
| 155 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 156 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 157 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 158 | * wrap. |
| 159 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 160 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 161 | * |
| 162 | * @return Sampler |
| 163 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 164 | public static Sampler WRAP_NEAREST(RenderScript rs) { |
| 165 | if(rs.mSampler_WRAP_NEAREST == null) { |
| 166 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 167 | b.setMinification(Value.NEAREST); |
| 168 | b.setMagnification(Value.NEAREST); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 169 | b.setWrapS(Value.WRAP); |
| 170 | b.setWrapT(Value.WRAP); |
| 171 | rs.mSampler_WRAP_NEAREST = b.create(); |
| 172 | } |
| 173 | return rs.mSampler_WRAP_NEAREST; |
| 174 | } |
| 175 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 176 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 177 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 178 | * wrap. |
| 179 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 180 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 181 | * |
| 182 | * @return Sampler |
| 183 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 184 | public static Sampler WRAP_LINEAR(RenderScript rs) { |
| 185 | if(rs.mSampler_WRAP_LINEAR == null) { |
| 186 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 187 | b.setMinification(Value.LINEAR); |
| 188 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 189 | b.setWrapS(Value.WRAP); |
| 190 | b.setWrapT(Value.WRAP); |
| 191 | rs.mSampler_WRAP_LINEAR = b.create(); |
| 192 | } |
| 193 | return rs.mSampler_WRAP_LINEAR; |
| 194 | } |
| 195 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 196 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 197 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 198 | * to and wrap modes set to wrap. |
| 199 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 200 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 201 | * |
| 202 | * @return Sampler |
| 203 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 204 | public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { |
| 205 | if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { |
| 206 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 207 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 208 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 209 | b.setWrapS(Value.WRAP); |
| 210 | b.setWrapT(Value.WRAP); |
| 211 | rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); |
| 212 | } |
| 213 | return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; |
| 214 | } |
| 215 | |
| 216 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 217 | /** |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 218 | * Builder for creating non-standard samplers. Usefull if mix and match of |
| 219 | * wrap modes is necesary or if anisotropic filtering is desired. |
| 220 | * |
| 221 | */ |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 222 | public static class Builder { |
| 223 | RenderScript mRS; |
| 224 | Value mMin; |
| 225 | Value mMag; |
| 226 | Value mWrapS; |
| 227 | Value mWrapT; |
| 228 | Value mWrapR; |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 229 | float mAniso; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 230 | |
| 231 | public Builder(RenderScript rs) { |
| 232 | mRS = rs; |
| 233 | mMin = Value.NEAREST; |
| 234 | mMag = Value.NEAREST; |
| 235 | mWrapS = Value.WRAP; |
| 236 | mWrapT = Value.WRAP; |
| 237 | mWrapR = Value.WRAP; |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 238 | mAniso = 1.0f; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 241 | public void setMinification(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 242 | if (v == Value.NEAREST || |
| 243 | v == Value.LINEAR || |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 244 | v == Value.LINEAR_MIP_LINEAR || |
| 245 | v == Value.LINEAR_MIP_NEAREST) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 246 | mMin = v; |
| 247 | } else { |
| 248 | throw new IllegalArgumentException("Invalid value"); |
| 249 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 252 | public void setMagnification(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 253 | if (v == Value.NEAREST || v == Value.LINEAR) { |
| 254 | mMag = v; |
| 255 | } else { |
| 256 | throw new IllegalArgumentException("Invalid value"); |
| 257 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | public void setWrapS(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 261 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 262 | mWrapS = v; |
| 263 | } else { |
| 264 | throw new IllegalArgumentException("Invalid value"); |
| 265 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | public void setWrapT(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 269 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 270 | mWrapT = v; |
| 271 | } else { |
| 272 | throw new IllegalArgumentException("Invalid value"); |
| 273 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 276 | public void setAnisotropy(float v) { |
| 277 | if(v >= 0.0f) { |
| 278 | mAniso = v; |
| 279 | } else { |
| 280 | throw new IllegalArgumentException("Invalid value"); |
| 281 | } |
| 282 | } |
| 283 | |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 284 | public Sampler create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 285 | mRS.validate(); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 286 | int id = mRS.nSamplerCreate(mMag.mID, mMin.mID, |
Alex Sakhartchouk | 7d5f5e7 | 2011-10-18 11:08:31 -0700 | [diff] [blame] | 287 | mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso); |
| 288 | Sampler sampler = new Sampler(id, mRS); |
| 289 | sampler.mMin = mMin; |
| 290 | sampler.mMag = mMag; |
| 291 | sampler.mWrapS = mWrapS; |
| 292 | sampler.mWrapT = mWrapT; |
| 293 | sampler.mWrapR = mWrapR; |
| 294 | sampler.mAniso = mAniso; |
| 295 | return sampler; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | |