Jason Sams | 2253417 | 2009-08-04 16:58:20 -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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import android.util.Config; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | |
| 24 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 25 | * <p>The Renderscript fragment program, also known as fragment shader is responsible |
| 26 | * for manipulating pixel data in a user defined way. It's constructed from a GLSL |
| 27 | * shader string containing the program body, textures inputs, and a Type object |
| 28 | * that describes the constants used by the program. Similar to the vertex programs, |
| 29 | * when an allocation with constant input values is bound to the shader, its values |
| 30 | * are sent to the graphics program automatically.</p> |
| 31 | * <p> The values inside the allocation are not explicitly tracked. If they change between two draw |
| 32 | * calls using the same program object, the runtime needs to be notified of that |
| 33 | * change by calling rsgAllocationSyncAll so it could send the new values to hardware. |
| 34 | * Communication between the vertex and fragment programs is handled internally in the |
| 35 | * GLSL code. For example, if the fragment program is expecting a varying input called |
| 36 | * varTex0, the GLSL code inside the program vertex must provide it. |
| 37 | * </p> |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 38 | * |
| 39 | **/ |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 40 | public class ProgramFragment extends Program { |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 41 | ProgramFragment(int id, RenderScript rs) { |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 42 | super(id, rs); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 45 | public static class Builder extends BaseProgramBuilder { |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Create a builder object. |
| 48 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 49 | * @param rs Context to which the program will belong. |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 50 | */ |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 51 | public Builder(RenderScript rs) { |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 52 | super(rs); |
| 53 | } |
| 54 | |
Alex Sakhartchouk | df27202 | 2011-01-09 11:34:03 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Creates ProgramFragment from the current state of the builder |
| 57 | * |
| 58 | * @return ProgramFragment |
| 59 | */ |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 60 | public ProgramFragment create() { |
| 61 | mRS.validate(); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 62 | int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2]; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 63 | int idx = 0; |
| 64 | |
| 65 | for (int i=0; i < mInputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 66 | tmp[idx++] = ProgramParam.INPUT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 67 | tmp[idx++] = mInputs[i].getID(); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 68 | } |
| 69 | for (int i=0; i < mOutputCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 70 | tmp[idx++] = ProgramParam.OUTPUT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 71 | tmp[idx++] = mOutputs[i].getID(); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 72 | } |
| 73 | for (int i=0; i < mConstantCount; i++) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 74 | tmp[idx++] = ProgramParam.CONSTANT.mID; |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 75 | tmp[idx++] = mConstants[i].getID(); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 76 | } |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 77 | for (int i=0; i < mTextureCount; i++) { |
| 78 | tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID; |
| 79 | tmp[idx++] = mTextureTypes[i].mID; |
| 80 | } |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 81 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 82 | int id = mRS.nProgramFragmentCreate(mShader, tmp); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 83 | ProgramFragment pf = new ProgramFragment(id, mRS); |
| 84 | initProgram(pf); |
| 85 | return pf; |
| 86 | } |
| 87 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | |