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 | |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 17 | /** |
| 18 | * <p>The Renderscript vertex program, also known as a vertex shader, describes a stage in |
| 19 | * the graphics pipeline responsible for manipulating geometric data in a user-defined way. |
| 20 | * The object is constructed by providing the Renderscript system with the following data:</p> |
| 21 | * <ul> |
| 22 | * <li>Element describing its varying inputs or attributes</li> |
| 23 | * <li>GLSL shader string that defines the body of the program</li> |
| 24 | * <li>a Type that describes the layout of an Allocation containing constant or uniform inputs</li> |
| 25 | * </ul> |
| 26 | * |
| 27 | * <p>Once the program is created, you bind it to the graphics context, RenderScriptGL, and it will be used for |
| 28 | * all subsequent draw calls until you bind a new program. If the program has constant inputs, |
| 29 | * the user needs to bind an allocation containing those inputs. The allocation's type must match |
| 30 | * the one provided during creation. The Renderscript library then does all the necessary plumbing |
| 31 | * to send those constants to the graphics hardware. Varying inputs to the shader, such as position, normal, |
| 32 | * and texture coordinates are matched by name between the input Element and the Mesh object being drawn. |
| 33 | * The signatures don't have to be exact or in any strict order. As long as the input name in the shader |
| 34 | * matches a channel name and size available on the mesh, the runtime takes care of connecting the |
| 35 | * two. Unlike OpenGL, there is no need to link the vertex and fragment programs.</p> |
| 36 | * |
| 37 | **/ |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 38 | package android.renderscript; |
| 39 | |
| 40 | |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 41 | import android.graphics.Matrix; |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 42 | import android.util.Config; |
| 43 | import android.util.Log; |
| 44 | |
| 45 | |
| 46 | /** |
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 | |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 54 | ProgramVertex(int 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 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Builder class for creating ProgramVertex objects. |
| 60 | * The builder starts empty and the user must minimally provide |
| 61 | * the GLSL shader code, and the varying inputs. Constant, or |
| 62 | * uniform parameters to the shader may optionally be provided as |
| 63 | * well. |
| 64 | * |
| 65 | **/ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 66 | public static class Builder extends BaseProgramBuilder { |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Create a builder object. |
| 69 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 70 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 71 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 72 | public Builder(RenderScript rs) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 73 | super(rs); |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Add varying inputs to the program |
| 78 | * |
| 79 | * @param e element describing the layout of the varying input |
| 80 | * structure |
| 81 | * @return self |
| 82 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 83 | public Builder addInput(Element e) throws IllegalStateException { |
| 84 | // Should check for consistant and non-conflicting names... |
| 85 | if(mInputCount >= MAX_INPUT) { |
| 86 | throw new RSIllegalArgumentException("Max input count exceeded."); |
| 87 | } |
| 88 | if (e.isComplex()) { |
| 89 | throw new RSIllegalArgumentException("Complex elements not allowed."); |
| 90 | } |
| 91 | mInputs[mInputCount++] = e; |
| 92 | return this; |
| 93 | } |
| 94 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Creates ProgramVertex from the current state of the builder |
| 97 | * |
| 98 | * @return ProgramVertex |
| 99 | */ |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 100 | public ProgramVertex create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 101 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 102 | int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 103 | int idx = 0; |
| 104 | |
| 105 | for (int i=0; i < mInputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 106 | tmp[idx++] = ProgramParam.INPUT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 107 | tmp[idx++] = mInputs[i].getID(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 108 | } |
| 109 | for (int i=0; i < mOutputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 110 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 111 | tmp[idx++] = mOutputs[i].getID(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 112 | } |
| 113 | for (int i=0; i < mConstantCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 114 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 115 | tmp[idx++] = mConstants[i].getID(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 116 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 117 | for (int i=0; i < mTextureCount; i++) { |
| 118 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
| 119 | tmp[idx++] = mTextureTypes[i].mID; |
| 120 | } |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 121 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 122 | int id = mRS.nProgramVertexCreate(mShader, tmp); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 123 | ProgramVertex pv = new ProgramVertex(id, mRS); |
| 124 | initProgram(pv); |
| 125 | return pv; |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 129 | } |