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