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 | |
Artur Satayev | 2ebb31c | 2020-01-08 12:24:36 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 20 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 21 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 22 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 23 | * @hide |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 24 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 25 | * ProgramVertexFixedFunction is a helper class that provides a |
| 26 | * simple way to create a fixed function emulation vertex shader |
| 27 | * without writing any GLSL code. |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 28 | * |
| 29 | **/ |
Xusong Wang | 8b4548c | 2021-01-05 10:09:52 -0800 | [diff] [blame^] | 30 | @Deprecated |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 31 | public class ProgramVertexFixedFunction extends ProgramVertex { |
| 32 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 33 | ProgramVertexFixedFunction(long id, RenderScript rs) { |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 34 | super(id, rs); |
| 35 | } |
| 36 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 37 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 38 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 39 | * Binds the constant buffer containing fixed function emulation |
| 40 | * matrices |
| 41 | * |
| 42 | * @param va allocation containing fixed function matrices |
| 43 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 44 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 45 | public void bindConstants(Constants va) { |
| 46 | mRS.validate(); |
| 47 | bindConstants(va.getAllocation(), 0); |
| 48 | } |
| 49 | |
| 50 | static class InternalBuilder extends BaseProgramBuilder { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 51 | /** |
| 52 | * @deprecated in API 16 |
| 53 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 54 | public InternalBuilder(RenderScript rs) { |
| 55 | super(rs); |
| 56 | } |
| 57 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @deprecated in API 16 |
| 60 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 61 | public InternalBuilder addInput(Element e) throws IllegalStateException { |
| 62 | // Should check for consistant and non-conflicting names... |
| 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 73 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 74 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 75 | * Creates ProgramVertexFixedFunction from the current state of |
| 76 | * the builder |
| 77 | * |
| 78 | * @return ProgramVertexFixedFunction |
| 79 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 80 | public ProgramVertexFixedFunction create() { |
| 81 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 82 | long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 83 | String[] texNames = new String[mTextureCount]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 84 | int idx = 0; |
| 85 | |
| 86 | for (int i=0; i < mInputCount; i++) { |
| 87 | tmp[idx++] = ProgramParam.INPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 88 | tmp[idx++] = mInputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 89 | } |
| 90 | for (int i=0; i < mOutputCount; i++) { |
| 91 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 92 | tmp[idx++] = mOutputs[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 93 | } |
| 94 | for (int i=0; i < mConstantCount; i++) { |
| 95 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 96 | tmp[idx++] = mConstants[i].getID(mRS); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 97 | } |
| 98 | for (int i=0; i < mTextureCount; i++) { |
| 99 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 100 | tmp[idx++] = mTextureTypes[i].mID; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 101 | texNames[i] = mTextureNames[i]; |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 104 | long id = mRS.nProgramVertexCreate(mShader, texNames, tmp); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 105 | ProgramVertexFixedFunction pv = new ProgramVertexFixedFunction(id, mRS); |
| 106 | initProgram(pv); |
| 107 | return pv; |
| 108 | } |
| 109 | } |
| 110 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 111 | /** |
| 112 | * @deprecated in API 16 |
| 113 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 114 | public static class Builder { |
| 115 | boolean mTextureMatrixEnable; |
| 116 | String mShader; |
| 117 | RenderScript mRS; |
| 118 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 119 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 120 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 121 | * Creates a builder for fixed function vertex program |
| 122 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 123 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 124 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 125 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 126 | public Builder(RenderScript rs) { |
| 127 | mRS = rs; |
| 128 | } |
| 129 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 130 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 131 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 132 | * Specifies whether texture matrix calculations are to be added |
| 133 | * to the shader |
| 134 | * |
| 135 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 171 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 172 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 173 | * Creates ProgramVertexFixedFunction from the current state of |
| 174 | * the builder |
| 175 | * |
| 176 | * @return Fixed function emulation ProgramVertex |
| 177 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 178 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 197 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 198 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 199 | * Helper class to store modelview, projection and texture |
| 200 | * matrices for ProgramVertexFixedFunction |
| 201 | * |
| 202 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 218 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 219 | * @deprecated in API 16 |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 220 | * Creates a buffer to store fixed function emulation matrices |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 221 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 222 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 223 | **/ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 224 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 225 | public Constants(RenderScript rs) { |
| 226 | Type constInputType = ProgramVertexFixedFunction.Builder.getConstantInputType(rs); |
| 227 | mAlloc = Allocation.createTyped(rs, constInputType); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 228 | int bufferSize = constInputType.getElement().getBytesSize()* |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 239 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 240 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 241 | * Forces deallocation of memory backing the contant matrices. |
| 242 | * Normally, this is unnecessary and will be garbage collected |
| 243 | * |
| 244 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | fa1275a | 2014-06-17 17:25:04 -0700 | [diff] [blame] | 255 | // Reset the buffer back to the end, since we want to flush all of |
| 256 | // the contents back (and not just what we wrote now). |
| 257 | mIOBuffer.reset(mIOBuffer.getData().length); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 258 | mAlloc.setFromFieldPacker(0, mIOBuffer); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 261 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 262 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 263 | * Sets the modelview matrix in the fixed function matrix buffer |
| 264 | * |
| 265 | * @param m modelview matrix |
| 266 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 267 | public void setModelview(Matrix4f m) { |
| 268 | mModel.load(m); |
| 269 | addToBuffer(MODELVIEW_OFFSET*4, m); |
| 270 | } |
| 271 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 272 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 273 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 274 | * Sets the projection matrix in the fixed function matrix buffer |
| 275 | * |
| 276 | * @param m projection matrix |
| 277 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 278 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 279 | public void setProjection(Matrix4f m) { |
| 280 | mProjection.load(m); |
| 281 | addToBuffer(PROJECTION_OFFSET*4, m); |
| 282 | } |
| 283 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 284 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 285 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 286 | * Sets the texture matrix in the fixed function matrix buffer. |
| 287 | * Texture matrix must be enabled in the |
| 288 | * ProgramVertexFixedFunction builder for the shader to utilize |
| 289 | * it. |
| 290 | * |
| 291 | * @param m modelview matrix |
| 292 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 293 | public void setTexture(Matrix4f m) { |
| 294 | mTexture.load(m); |
| 295 | addToBuffer(TEXTURE_OFFSET*4, m); |
| 296 | } |
| 297 | } |
| 298 | } |