Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 17 | /** |
| 18 | * @hide |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 19 | * <p>The RenderScript vertex program, also known as a vertex shader, describes a stage in |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 20 | * the graphics pipeline responsible for manipulating geometric data in a user-defined way. |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 21 | * The object is constructed by providing the RenderScript system with the following data:</p> |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 22 | * <ul> |
| 23 | * <li>Element describing its varying inputs or attributes</li> |
| 24 | * <li>GLSL shader string that defines the body of the program</li> |
| 25 | * <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li> |
| 26 | * </ul> |
| 27 | * |
| 28 | * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for |
| 29 | * all subsequent draw calls until you bind a new program. If the program has constant inputs, |
| 30 | * the user needs to bind an allocation containing those inputs. The allocation's type must match |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 31 | * the one provided during creation. The RenderScript library then does all the necessary plumbing |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 32 | * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal, |
| 33 | * and texture coordinates are matched by name between the input Element and the Mesh object being drawn. |
| 34 | * The signatures don't have to be exact or in any strict order. As long as the input name in the shader |
| 35 | * matches a channel name and size available on the mesh, the runtime takes care of connecting the |
| 36 | * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p> |
| 37 | * |
| 38 | **/ |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 39 | package android.renderscript; |
| 40 | |
Artur Satayev | 2ebb31c | 2020-01-08 12:24:36 +0000 | [diff] [blame] | 41 | import android.compat.annotation.UnsupportedAppUsage; |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 42 | |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 43 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 44 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 45 | * @hide |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 46 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 47 | * ProgramVertex, also know as a vertex shader, describes a |
| 48 | * stage in the graphics pipeline responsible for manipulating |
| 49 | * geometric data in a user-defined way. |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 50 | * |
| 51 | **/ |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 52 | public class ProgramVertex extends Program { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 53 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 54 | ProgramVertex(long id, RenderScript rs) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 55 | super(id, rs); |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 58 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 59 | * @deprecated in API 16 |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 60 | * @return number of input attribute elements |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 61 | */ |
| 62 | public int getInputCount() { |
| 63 | return mInputs != null ? mInputs.length : 0; |
| 64 | } |
| 65 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 66 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 67 | * @deprecated in API 16 |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 68 | * @param slot location of the input to return |
| 69 | * @return input attribute element |
Alex Sakhartchouk | d5a62bb | 2012-01-06 10:36:06 -0800 | [diff] [blame] | 70 | */ |
| 71 | public Element getInput(int slot) { |
| 72 | if (slot < 0 || slot >= mInputs.length) { |
| 73 | throw new IllegalArgumentException("Slot ID out of range."); |
| 74 | } |
| 75 | return mInputs[slot]; |
| 76 | } |
| 77 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 78 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 79 | * @hide |
| 80 | * @deprecated in API 16 |
| 81 | * Builder class for creating ProgramVertex objects. |
| 82 | * The builder starts empty and the user must minimally provide |
| 83 | * the GLSL shader code, and the varying inputs. Constant, or |
| 84 | * uniform parameters to the shader may optionally be provided as |
| 85 | * well. |
| 86 | * |
| 87 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 88 | public static class Builder extends BaseProgramBuilder { |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 89 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 90 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 91 | * Create a builder object. |
| 92 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 93 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 94 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 95 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 96 | public Builder(RenderScript rs) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 97 | super(rs); |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 100 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 101 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 102 | * Add varying inputs to the program |
| 103 | * |
| 104 | * @param e element describing the layout of the varying input |
| 105 | * structure |
| 106 | * @return self |
| 107 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 108 | @UnsupportedAppUsage |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 109 | public Builder addInput(Element e) throws IllegalStateException { |
| 110 | // Should check for consistant and non-conflicting names... |
| 111 | if(mInputCount >= MAX_INPUT) { |
| 112 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 113 | } |
| 114 | if (e.isComplex()) { |
| 115 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
| 116 | } |
| 117 | mInputs[mInputCount++] = e; |
| 118 | return this; |
| 119 | } |
| 120 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 121 | /** |
Jason Sams | d4ca991 | 2012-05-08 19:02:07 -0700 | [diff] [blame] | 122 | * @deprecated in API 16 |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 123 | * Creates ProgramVertex from the current state of the builder |
| 124 | * |
| 125 | * @return ProgramVertex |
| 126 | */ |
Mathew Inwood | 1532447 | 2018-08-06 11:18:49 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 128 | public ProgramVertex create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 129 | mRS.validate(); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 130 | long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 131 | String[] texNames = new String[mTextureCount]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 132 | int idx = 0; |
| 133 | |
| 134 | for (int i=0; i < mInputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 135 | tmp[idx++] = ProgramParam.INPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 136 | tmp[idx++] = mInputs[i].getID(mRS); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 137 | } |
| 138 | for (int i=0; i < mOutputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 139 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 140 | tmp[idx++] = mOutputs[i].getID(mRS); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 141 | } |
| 142 | for (int i=0; i < mConstantCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 143 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 144 | tmp[idx++] = mConstants[i].getID(mRS); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 145 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 146 | for (int i=0; i < mTextureCount; i++) { |
| 147 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 148 | tmp[idx++] = mTextureTypes[i].mID; |
Alex Sakhartchouk | 2123b46 | 2012-02-15 16:21:46 -0800 | [diff] [blame] | 149 | texNames[i] = mTextureNames[i]; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 150 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 151 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 152 | long id = mRS.nProgramVertexCreate(mShader, texNames, tmp); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 153 | ProgramVertex pv = new ProgramVertex(id, mRS); |
| 154 | initProgram(pv); |
| 155 | return pv; |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 159 | } |