Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 20 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 21 | * @hide |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 22 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 23 | * ProgramVertexFixedFunction is a helper class that provides a |
| 24 | * simple way to create a fixed function emulation vertex shader |
| 25 | * without writing any GLSL code. |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 26 | * |
| 27 | **/ |
| 28 | public class ProgramVertexFixedFunction extends ProgramVertex { |
| 29 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 30 | ProgramVertexFixedFunction(long id, RenderScript rs) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 31 | super(id, rs); |
| 32 | } |
| 33 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 34 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 35 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 36 | * Binds the constant buffer containing fixed function emulation |
| 37 | * matrices |
| 38 | * |
| 39 | * @param va allocation containing fixed function matrices |
| 40 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 41 | public void bindConstants(Constants va) { |
| 42 | mRS.validate(); |
| 43 | bindConstants(va.getAllocation(), 0); |
| 44 | } |
| 45 | |
| 46 | static class InternalBuilder extends BaseProgramBuilder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 47 | /** |
| 48 | * @deprecated in API 16 |
| 49 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 50 | public InternalBuilder(RenderScript rs) { |
| 51 | super(rs); |
| 52 | } |
| 53 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @deprecated in API 16 |
| 56 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 57 | public InternalBuilder addInput(Element e) throws IllegalStateException { |
| 58 | // Should check for consistant and non-conflicting names... |
| 59 | if(mInputCount >= MAX_INPUT) { |
| 60 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 61 | } |
| 62 | if (e.isComplex()) { |
| 63 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
| 64 | } |
| 65 | mInputs[mInputCount++] = e; |
| 66 | return this; |
| 67 | } |
| 68 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 69 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 70 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 71 | * Creates ProgramVertexFixedFunction from the current state of |
| 72 | * the builder |
| 73 | * |
| 74 | * @return ProgramVertexFixedFunction |
| 75 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 76 | public ProgramVertexFixedFunction create() { |
| 77 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 78 | long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 79 | String[] texNames = new String[mTextureCount]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 80 | int idx = 0; |
| 81 | |
| 82 | for (int i=0; i < mInputCount; i++) { |
| 83 | tmp[idx++] = ProgramParam.INPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 84 | tmp[idx++] = mInputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 85 | } |
| 86 | for (int i=0; i < mOutputCount; i++) { |
| 87 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 88 | tmp[idx++] = mOutputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 89 | } |
| 90 | for (int i=0; i < mConstantCount; i++) { |
| 91 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 92 | tmp[idx++] = mConstants[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 93 | } |
| 94 | for (int i=0; i < mTextureCount; i++) { |
| 95 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 96 | tmp[idx++] = mTextureTypes[i].mID; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 97 | texNames[i] = mTextureNames[i]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 100 | long id = mRS.nProgramVertexCreate(mShader, texNames, tmp); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 101 | ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS); |
| 102 | initProgram(pv); |
| 103 | return pv; |
| 104 | } |
| 105 | } |
| 106 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 107 | /** |
| 108 | * @deprecated in API 16 |
| 109 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 110 | public static class Builder { |
| 111 | boolean mTextureMatrixEnable; |
| 112 | String mShader; |
| 113 | RenderScript mRS; |
| 114 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 115 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 116 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 117 | * Creates a builder for fixed function vertex program |
| 118 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 119 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 120 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 121 | public Builder(RenderScript rs) { |
| 122 | mRS = rs; |
| 123 | } |
| 124 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 125 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 126 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 127 | * Specifies whether texture matrix calculations are to be added |
| 128 | * to the shader |
| 129 | * |
| 130 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 131 | public Builder setTextureMatrixEnable(boolean enable) { |
| 132 | mTextureMatrixEnable = enable; |
| 133 | return this; |
| 134 | } |
| 135 | static Type getConstantInputType(RenderScript rs) { |
| 136 | Element.Builder b = new Element.Builder(rs); |
| 137 | b.add(Element.MATRIX4X4(rs), "MV"); |
| 138 | b.add(Element.MATRIX4X4(rs), "P"); |
| 139 | b.add(Element.MATRIX4X4(rs), "TexMatrix"); |
| 140 | b.add(Element.MATRIX4X4(rs), "MVP"); |
| 141 | |
| 142 | Type.Builder typeBuilder = new Type.Builder(rs, b.create()); |
| 143 | typeBuilder.setX(1); |
| 144 | return typeBuilder.create(); |
| 145 | } |
| 146 | |
| 147 | private void buildShaderString() { |
| 148 | |
| 149 | mShader = "//rs_shader_internal\n"; |
| 150 | mShader += "varying vec4 varColor;\n"; |
| 151 | mShader += "varying vec2 varTex0;\n"; |
| 152 | |
| 153 | mShader += "void main() {\n"; |
| 154 | mShader += " gl_Position = UNI_MVP * ATTRIB_position;\n"; |
| 155 | mShader += " gl_PointSize = 1.0;\n"; |
| 156 | |
| 157 | mShader += " varColor = ATTRIB_color;\n"; |
| 158 | if (mTextureMatrixEnable) { |
| 159 | mShader += " varTex0 = (UNI_TexMatrix * vec4(ATTRIB_texture0, 0.0, 1.0)).xy;\n"; |
| 160 | } else { |
| 161 | mShader += " varTex0 = ATTRIB_texture0;\n"; |
| 162 | } |
| 163 | mShader += "}\n"; |
| 164 | } |
| 165 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 166 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 167 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 168 | * Creates ProgramVertexFixedFunction from the current state of |
| 169 | * the builder |
| 170 | * |
| 171 | * @return Fixed function emulation ProgramVertex |
| 172 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 173 | public ProgramVertexFixedFunction create() { |
| 174 | buildShaderString(); |
| 175 | |
| 176 | InternalBuilder sb = new InternalBuilder(mRS); |
| 177 | sb.setShader(mShader); |
| 178 | sb.addConstant(getConstantInputType(mRS)); |
| 179 | |
| 180 | Element.Builder b = new Element.Builder(mRS); |
| 181 | b.add(Element.F32_4(mRS), "position"); |
| 182 | b.add(Element.F32_4(mRS), "color"); |
| 183 | b.add(Element.F32_3(mRS), "normal"); |
| 184 | b.add(Element.F32_2(mRS), "texture0"); |
| 185 | sb.addInput(b.create()); |
| 186 | |
| 187 | return sb.create(); |
| 188 | } |
| 189 | } |
| 190 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 191 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 192 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 193 | * Helper class to store modelview, projection and texture |
| 194 | * matrices for ProgramVertexFixedFunction |
| 195 | * |
| 196 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 197 | public static class Constants { |
| 198 | static final int MODELVIEW_OFFSET = 0; |
| 199 | static final int PROJECTION_OFFSET = 16; |
| 200 | static final int TEXTURE_OFFSET = 32; |
| 201 | |
| 202 | Matrix4f mModel; |
| 203 | Matrix4f mProjection; |
| 204 | Matrix4f mTexture; |
| 205 | |
| 206 | Allocation mAlloc; |
| 207 | Allocation getAllocation() { |
| 208 | return mAlloc; |
| 209 | } |
| 210 | private FieldPacker mIOBuffer; |
| 211 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 212 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 213 | * @deprecated in API 16 |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 214 | * Creates a buffer to store fixed function emulation matrices |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 215 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 216 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 217 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 218 | public Constants(RenderScript rs) { |
| 219 | Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs); |
| 220 | mAlloc = Allocation.createTyped(rs, constInputType); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 221 | int bufferSize = constInputType.getElement().getBytesSize()* |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 222 | constInputType.getCount(); |
| 223 | mIOBuffer = new FieldPacker(bufferSize); |
| 224 | mModel = new Matrix4f(); |
| 225 | mProjection = new Matrix4f(); |
| 226 | mTexture = new Matrix4f(); |
| 227 | setModelview(new Matrix4f()); |
| 228 | setProjection(new Matrix4f()); |
| 229 | setTexture(new Matrix4f()); |
| 230 | } |
| 231 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 232 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 233 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 234 | * Forces deallocation of memory backing the contant matrices. |
| 235 | * Normally, this is unnecessary and will be garbage collected |
| 236 | * |
| 237 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 238 | public void destroy() { |
| 239 | mAlloc.destroy(); |
| 240 | mAlloc = null; |
| 241 | } |
| 242 | |
| 243 | private void addToBuffer(int offset, Matrix4f m) { |
| 244 | mIOBuffer.reset(offset); |
| 245 | for(int i = 0; i < 16; i ++) { |
| 246 | mIOBuffer.addF32(m.mMat[i]); |
| 247 | } |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 248 | mAlloc.setFromFieldPacker(0, mIOBuffer); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 251 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 252 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 253 | * Sets the modelview matrix in the fixed function matrix buffer |
| 254 | * |
| 255 | * @param m modelview matrix |
| 256 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 257 | public void setModelview(Matrix4f m) { |
| 258 | mModel.load(m); |
| 259 | addToBuffer(MODELVIEW_OFFSET*4, m); |
| 260 | } |
| 261 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 262 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 263 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 264 | * Sets the projection matrix in the fixed function matrix buffer |
| 265 | * |
| 266 | * @param m projection matrix |
| 267 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 268 | public void setProjection(Matrix4f m) { |
| 269 | mProjection.load(m); |
| 270 | addToBuffer(PROJECTION_OFFSET*4, m); |
| 271 | } |
| 272 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 273 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 274 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 275 | * Sets the texture matrix in the fixed function matrix buffer. |
| 276 | * Texture matrix must be enabled in the |
| 277 | * ProgramVertexFixedFunction builder for the shader to utilize |
| 278 | * it. |
| 279 | * |
| 280 | * @param m modelview matrix |
| 281 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 282 | public void setTexture(Matrix4f m) { |
| 283 | mTexture.load(m); |
| 284 | addToBuffer(TEXTURE_OFFSET*4, m); |
| 285 | } |
| 286 | } |
| 287 | } |