| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.renderscript; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
@Deprecated |
| 31 |
public class ProgramVertexFixedFunction extends ProgramVertex { |
| 32 |
|
| 33 |
ProgramVertexFixedFunction(long id, RenderScript rs) { |
| 34 |
super(id, rs); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
@UnsupportedAppUsage |
| 45 |
public void bindConstants(Constants va) { |
| 46 |
mRS.validate(); |
| 47 |
bindConstants(va.getAllocation(), 0); |
| 48 |
} |
| 49 |
|
| 50 |
static class InternalBuilder extends BaseProgramBuilder { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public InternalBuilder(RenderScript rs) { |
| 55 |
super(rs); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public InternalBuilder addInput(Element e) throws IllegalStateException { |
| 62 |
|
| 63 |
if(mInputCount >= MAX_INPUT) { |
| 64 |
throw new RSIllegalArgumentException("Max input count exceeded."); |
| 65 |
} |
| 66 |
if (e.isComplex()) { |
| 67 |
throw new RSIllegalArgumentException("Complex elements not allowed."); |
| 68 |
} |
| 69 |
mInputs[mInputCount++] = e; |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public ProgramVertexFixedFunction create() { |
| 81 |
mRS.validate(); |
| 82 |
long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
| 83 |
String[] texNames = new String[mTextureCount]; |
| 84 |
int idx = 0; |
| 85 |
|
| 86 |
for (int i=0; i < mInputCount; i++) { |
| 87 |
tmp[idx++] = ProgramParam.INPUT.mID; |
| 88 |
tmp[idx++] = mInputs[i].getID(mRS); |
| 89 |
} |
| 90 |
for (int i=0; i < mOutputCount; i++) { |
| 91 |
tmp[idx++] = ProgramParam.OUTPUT.mID; |
| 92 |
tmp[idx++] = mOutputs[i].getID(mRS); |
| 93 |
} |
| 94 |
for (int i=0; i < mConstantCount; i++) { |
| 95 |
tmp[idx++] = ProgramParam.CONSTANT.mID; |
| 96 |
tmp[idx++] = mConstants[i].getID(mRS); |
| 97 |
} |
| 98 |
for (int i=0; i < mTextureCount; i++) { |
| 99 |
tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
| 100 |
tmp[idx++] = mTextureTypes[i].mID; |
| 101 |
texNames[i] = mTextureNames[i]; |
| 102 |
} |
| 103 |
|
| 104 |
long id = mRS.nProgramVertexCreate(mShader, texNames, tmp); |
| 105 |
ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS); |
| 106 |
initProgram(pv); |
| 107 |
return pv; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static class Builder { |
| 115 |
boolean mTextureMatrixEnable; |
| 116 |
String mShader; |
| 117 |
RenderScript mRS; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@UnsupportedAppUsage |
| 126 |
public Builder(RenderScript rs) { |
| 127 |
mRS = rs; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public Builder setTextureMatrixEnable(boolean enable) { |
| 137 |
mTextureMatrixEnable = enable; |
| 138 |
return this; |
| 139 |
} |
| 140 |
static Type getConstantInputType(RenderScript rs) { |
| 141 |
Element.Builder b = new Element.Builder(rs); |
| 142 |
b.add(Element.MATRIX4X4(rs), "MV"); |
| 143 |
b.add(Element.MATRIX4X4(rs), "P"); |
| 144 |
b.add(Element.MATRIX4X4(rs), "TexMatrix"); |
| 145 |
b.add(Element.MATRIX4X4(rs), "MVP"); |
| 146 |
|
| 147 |
Type.Builder typeBuilder = new Type.Builder(rs, b.create()); |
| 148 |
typeBuilder.setX(1); |
| 149 |
return typeBuilder.create(); |
| 150 |
} |
| 151 |
|
| 152 |
private void buildShaderString() { |
| 153 |
|
| 154 |
mShader = "//rs_shader_internal\n"; |
| 155 |
mShader += "varying vec4 varColor;\n"; |
| 156 |
mShader += "varying vec2 varTex0;\n"; |
| 157 |
|
| 158 |
mShader += "void main() {\n"; |
| 159 |
mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n"; |
| 160 |
mShader += " gl_PointSize = 1.0;\n"; |
| 161 |
|
| 162 |
mShader += " varColor = ATTRIB_color;\n"; |
| 163 |
if (mTextureMatrixEnable) { |
| 164 |
mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n"; |
| 165 |
} else { |
| 166 |
mShader += " varTex0 = ATTRIB_texture0;\n"; |
| 167 |
} |
| 168 |
mShader += "}\n"; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@UnsupportedAppUsage |
| 179 |
public ProgramVertexFixedFunction create() { |
| 180 |
buildShaderString(); |
| 181 |
|
| 182 |
InternalBuilder sb = new InternalBuilder(mRS); |
| 183 |
sb.setShader(mShader); |
| 184 |
sb.addConstant(getConstantInputType(mRS)); |
| 185 |
|
| 186 |
Element.Builder b = new Element.Builder(mRS); |
| 187 |
b.add(Element.F32_4(mRS), "position"); |
| 188 |
b.add(Element.F32_4(mRS), "color"); |
| 189 |
b.add(Element.F32_3(mRS), "normal"); |
| 190 |
b.add(Element.F32_2(mRS), "texture0"); |
| 191 |
sb.addInput(b.create()); |
| 192 |
|
| 193 |
return sb.create(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static class Constants { |
| 204 |
static final int MODELVIEW_OFFSET = 0; |
| 205 |
static final int PROJECTION_OFFSET = 16; |
| 206 |
static final int TEXTURE_OFFSET = 32; |
| 207 |
|
| 208 |
Matrix4f mModel; |
| 209 |
Matrix4f mProjection; |
| 210 |
Matrix4f mTexture; |
| 211 |
|
| 212 |
Allocation mAlloc; |
| 213 |
Allocation getAllocation() { |
| 214 |
return mAlloc; |
| 215 |
} |
| 216 |
private FieldPacker mIOBuffer; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
@UnsupportedAppUsage |
| 225 |
public Constants(RenderScript rs) { |
| 226 |
Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs); |
| 227 |
mAlloc = Allocation.createTyped(rs, constInputType); |
| 228 |
int bufferSize = constInputType.getElement().getBytesSize()* |
| 229 |
constInputType.getCount(); |
| 230 |
mIOBuffer = new FieldPacker(bufferSize); |
| 231 |
mModel = new Matrix4f(); |
| 232 |
mProjection = new Matrix4f(); |
| 233 |
mTexture = new Matrix4f(); |
| 234 |
setModelview(new Matrix4f()); |
| 235 |
setProjection(new Matrix4f()); |
| 236 |
setTexture(new Matrix4f()); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public void destroy() { |
| 246 |
mAlloc.destroy(); |
| 247 |
mAlloc = null; |
| 248 |
} |
| 249 |
|
| 250 |
private void addToBuffer(int offset, Matrix4f m) { |
| 251 |
mIOBuffer.reset(offset); |
| 252 |
for(int i = 0; i < 16; i ++) { |
| 253 |
mIOBuffer.addF32(m.mMat[i]); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
mIOBuffer.reset(mIOBuffer.getData().length); |
| 258 |
mAlloc.setFromFieldPacker(0, mIOBuffer); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public void setModelview(Matrix4f m) { |
| 268 |
mModel.load(m); |
| 269 |
addToBuffer(MODELVIEW_OFFSET*4, m); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@UnsupportedAppUsage |
| 279 |
public void setProjection(Matrix4f m) { |
| 280 |
mProjection.load(m); |
| 281 |
addToBuffer(PROJECTION_OFFSET*4, m); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public void setTexture(Matrix4f m) { |
| 294 |
mTexture.load(m); |
| 295 |
addToBuffer(TEXTURE_OFFSET*4, m); |
| 296 |
} |
| 297 |
} |
| 298 |
} |