| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.renderscript; |
| 18 |
|
| 19 |
|
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.res.Resources; |
| 22 |
import android.util.Log; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.io.InputStream; |
| 26 |
import java.io.UnsupportedEncodingException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
@Deprecated |
| 40 |
public class Program extends BaseObj { |
| 41 |
static final int MAX_INPUT = 8; |
| 42 |
static final int MAX_OUTPUT = 8; |
| 43 |
static final int MAX_CONSTANT = 8; |
| 44 |
static final int MAX_TEXTURE = 8; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public enum TextureType { |
| 53 |
@UnsupportedAppUsage |
| 54 |
TEXTURE_2D (0), |
| 55 |
TEXTURE_CUBE (1); |
| 56 |
|
| 57 |
int mID; |
| 58 |
TextureType(int id) { |
| 59 |
mID = id; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
enum ProgramParam { |
| 64 |
INPUT (0), |
| 65 |
OUTPUT (1), |
| 66 |
CONSTANT (2), |
| 67 |
TEXTURE_TYPE (3); |
| 68 |
|
| 69 |
int mID; |
| 70 |
ProgramParam(int id) { |
| 71 |
mID = id; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
Element mInputs[]; |
| 76 |
Element mOutputs[]; |
| 77 |
Type mConstants[]; |
| 78 |
TextureType mTextures[]; |
| 79 |
String mTextureNames[]; |
| 80 |
int mTextureCount; |
| 81 |
String mShader; |
| 82 |
|
| 83 |
Program(long id, RenderScript rs) { |
| 84 |
super(id, rs); |
| 85 |
guard.open("destroy"); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public int getConstantCount() { |
| 94 |
return mConstants != null ? mConstants.length : 0; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public Type getConstant(int slot) { |
| 105 |
if (slot < 0 || slot >= mConstants.length) { |
| 106 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 107 |
} |
| 108 |
return mConstants[slot]; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public int getTextureCount() { |
| 116 |
return mTextureCount; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public TextureType getTextureType(int slot) { |
| 125 |
if ((slot < 0) || (slot >= mTextureCount)) { |
| 126 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 127 |
} |
| 128 |
return mTextures[slot]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public String getTextureName(int slot) { |
| 138 |
if ((slot < 0) || (slot >= mTextureCount)) { |
| 139 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 140 |
} |
| 141 |
return mTextureNames[slot]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public void bindConstants(Allocation a, int slot) { |
| 153 |
if (slot < 0 || slot >= mConstants.length) { |
| 154 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 155 |
} |
| 156 |
if (a != null && |
| 157 |
a.getType().getID(mRS) != mConstants[slot].getID(mRS)) { |
| 158 |
throw new IllegalArgumentException("Allocation type does not match slot type."); |
| 159 |
} |
| 160 |
long id = a != null ? a.getID(mRS) : 0; |
| 161 |
mRS.nProgramBindConstants(getID(mRS), slot, id); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void bindTexture(Allocation va, int slot) |
| 172 |
throws IllegalArgumentException { |
| 173 |
mRS.validate(); |
| 174 |
if ((slot < 0) || (slot >= mTextureCount)) { |
| 175 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 176 |
} |
| 177 |
if (va != null && va.getType().hasFaces() && |
| 178 |
mTextures[slot] != TextureType.TEXTURE_CUBE) { |
| 179 |
throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot"); |
| 180 |
} |
| 181 |
|
| 182 |
long id = va != null ? va.getID(mRS) : 0; |
| 183 |
mRS.nProgramBindTexture(getID(mRS), slot, id); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void bindSampler(Sampler vs, int slot) |
| 196 |
throws IllegalArgumentException { |
| 197 |
mRS.validate(); |
| 198 |
if ((slot < 0) || (slot >= mTextureCount)) { |
| 199 |
throw new IllegalArgumentException("Slot ID out of range."); |
| 200 |
} |
| 201 |
|
| 202 |
long id = vs != null ? vs.getID(mRS) : 0; |
| 203 |
mRS.nProgramBindSampler(getID(mRS), slot, id); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
public static class BaseProgramBuilder { |
| 208 |
@UnsupportedAppUsage |
| 209 |
RenderScript mRS; |
| 210 |
@UnsupportedAppUsage |
| 211 |
Element mInputs[]; |
| 212 |
@UnsupportedAppUsage |
| 213 |
Element mOutputs[]; |
| 214 |
@UnsupportedAppUsage |
| 215 |
Type mConstants[]; |
| 216 |
Type mTextures[]; |
| 217 |
TextureType mTextureTypes[]; |
| 218 |
String mTextureNames[]; |
| 219 |
@UnsupportedAppUsage |
| 220 |
int mInputCount; |
| 221 |
@UnsupportedAppUsage |
| 222 |
int mOutputCount; |
| 223 |
@UnsupportedAppUsage |
| 224 |
int mConstantCount; |
| 225 |
@UnsupportedAppUsage |
| 226 |
int mTextureCount; |
| 227 |
@UnsupportedAppUsage |
| 228 |
String mShader; |
| 229 |
|
| 230 |
|
| 231 |
@UnsupportedAppUsage |
| 232 |
protected BaseProgramBuilder(RenderScript rs) { |
| 233 |
mRS = rs; |
| 234 |
mInputs = new Element[MAX_INPUT]; |
| 235 |
mOutputs = new Element[MAX_OUTPUT]; |
| 236 |
mConstants = new Type[MAX_CONSTANT]; |
| 237 |
mInputCount = 0; |
| 238 |
mOutputCount = 0; |
| 239 |
mConstantCount = 0; |
| 240 |
mTextureCount = 0; |
| 241 |
mTextureTypes = new TextureType[MAX_TEXTURE]; |
| 242 |
mTextureNames = new String[MAX_TEXTURE]; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public BaseProgramBuilder setShader(String s) { |
| 252 |
mShader = s; |
| 253 |
return this; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public BaseProgramBuilder setShader(Resources resources, int resourceID) { |
| 265 |
byte[] str; |
| 266 |
int strLength; |
| 267 |
InputStream is = resources.openRawResource(resourceID); |
| 268 |
try { |
| 269 |
try { |
| 270 |
str = new byte[1024]; |
| 271 |
strLength = 0; |
| 272 |
while(true) { |
| 273 |
int bytesLeft = str.length - strLength; |
| 274 |
if (bytesLeft == 0) { |
| 275 |
byte[] buf2 = new byte[str.length * 2]; |
| 276 |
System.arraycopy(str, 0, buf2, 0, str.length); |
| 277 |
str = buf2; |
| 278 |
bytesLeft = str.length - strLength; |
| 279 |
} |
| 280 |
int bytesRead = is.read(str, strLength, bytesLeft); |
| 281 |
if (bytesRead <= 0) { |
| 282 |
break; |
| 283 |
} |
| 284 |
strLength += bytesRead; |
| 285 |
} |
| 286 |
} finally { |
| 287 |
is.close(); |
| 288 |
} |
| 289 |
} catch(IOException e) { |
| 290 |
throw new Resources.NotFoundException(); |
| 291 |
} |
| 292 |
|
| 293 |
try { |
| 294 |
mShader = new String(str, 0, strLength, "UTF-8"); |
| 295 |
} catch (UnsupportedEncodingException e) { |
| 296 |
Log.e("RenderScript shader creation", "Could not decode shader string"); |
| 297 |
} |
| 298 |
|
| 299 |
return this; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public int getCurrentConstantIndex() { |
| 307 |
return mConstantCount - 1; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public int getCurrentTextureIndex() { |
| 315 |
return mTextureCount - 1; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public BaseProgramBuilder addConstant(Type t) throws IllegalStateException { |
| 326 |
|
| 327 |
if(mConstantCount >= MAX_CONSTANT) { |
| 328 |
throw new RSIllegalArgumentException("Max input count exceeded."); |
| 329 |
} |
| 330 |
if (t.getElement().isComplex()) { |
| 331 |
throw new RSIllegalArgumentException("Complex elements not allowed."); |
| 332 |
} |
| 333 |
mConstants[mConstantCount] = t; |
| 334 |
mConstantCount++; |
| 335 |
return this; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException { |
| 346 |
addTexture(texType, "Tex" + mTextureCount); |
| 347 |
return this; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public BaseProgramBuilder addTexture(TextureType texType, String texName) |
| 360 |
throws IllegalArgumentException { |
| 361 |
if(mTextureCount >= MAX_TEXTURE) { |
| 362 |
throw new IllegalArgumentException("Max texture count exceeded."); |
| 363 |
} |
| 364 |
mTextureTypes[mTextureCount] = texType; |
| 365 |
mTextureNames[mTextureCount] = texName; |
| 366 |
mTextureCount ++; |
| 367 |
return this; |
| 368 |
} |
| 369 |
|
| 370 |
protected void initProgram(Program p) { |
| 371 |
p.mInputs = new Element[mInputCount]; |
| 372 |
System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount); |
| 373 |
p.mOutputs = new Element[mOutputCount]; |
| 374 |
System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount); |
| 375 |
p.mConstants = new Type[mConstantCount]; |
| 376 |
System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount); |
| 377 |
p.mTextureCount = mTextureCount; |
| 378 |
p.mTextures = new TextureType[mTextureCount]; |
| 379 |
System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount); |
| 380 |
p.mTextureNames = new String[mTextureCount]; |
| 381 |
System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
|