blob: 21bace82f74f828a0ec332df2eb1c482fa5981cb [file] [log] [blame]
Jason Sams22534172009-08-04 16:58:20 -07001/*
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
17package android.renderscript;
18
19
Jason Sams22534172009-08-04 16:58:20 -070020import android.util.Log;
21
22
23/**
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>The Renderscript fragment program, also known as fragment shader is responsible
25 * for manipulating pixel data in a user defined way. It's constructed from a GLSL
26 * shader string containing the program body, textures inputs, and a Type object
27 * that describes the constants used by the program. Similar to the vertex programs,
28 * when an allocation with constant input values is bound to the shader, its values
29 * are sent to the graphics program automatically.</p>
30 * <p> The values inside the allocation are not explicitly tracked. If they change between two draw
31 * calls using the same program object, the runtime needs to be notified of that
32 * change by calling rsgAllocationSyncAll so it could send the new values to hardware.
33 * Communication between the vertex and fragment programs is handled internally in the
34 * GLSL code. For example, if the fragment program is expecting a varying input called
35 * varTex0, the GLSL code inside the program vertex must provide it.
36 * </p>
Jason Sams22534172009-08-04 16:58:20 -070037 *
38 **/
Jason Sams7e5ab3b2009-12-15 13:27:04 -080039public class ProgramFragment extends Program {
Jason Sams22534172009-08-04 16:58:20 -070040 ProgramFragment(int id, RenderScript rs) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -080041 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -070042 }
43
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080044 public static class Builder extends BaseProgramBuilder {
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080045 /**
46 * Create a builder object.
47 *
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -080048 * @param rs Context to which the program will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080049 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080050 public Builder(RenderScript rs) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -080051 super(rs);
52 }
53
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080054 /**
55 * Creates ProgramFragment from the current state of the builder
56 *
57 * @return ProgramFragment
58 */
Jason Sams7e5ab3b2009-12-15 13:27:04 -080059 public ProgramFragment create() {
60 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080061 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080062 int idx = 0;
63
64 for (int i=0; i < mInputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080065 tmp[idx++] = ProgramParam.INPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080066 tmp[idx++] = mInputs[i].getID();
Jason Sams7e5ab3b2009-12-15 13:27:04 -080067 }
68 for (int i=0; i < mOutputCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080069 tmp[idx++] = ProgramParam.OUTPUT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080070 tmp[idx++] = mOutputs[i].getID();
Jason Sams7e5ab3b2009-12-15 13:27:04 -080071 }
72 for (int i=0; i < mConstantCount; i++) {
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080073 tmp[idx++] = ProgramParam.CONSTANT.mID;
Jason Sams06d69de2010-11-09 17:11:40 -080074 tmp[idx++] = mConstants[i].getID();
Jason Sams7e5ab3b2009-12-15 13:27:04 -080075 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080076 for (int i=0; i < mTextureCount; i++) {
77 tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
78 tmp[idx++] = mTextureTypes[i].mID;
79 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -080080
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070081 int id = mRS.nProgramFragmentCreate(mShader, tmp);
Jason Sams7e5ab3b2009-12-15 13:27:04 -080082 ProgramFragment pf = new ProgramFragment(id, mRS);
83 initProgram(pf);
84 return pf;
85 }
86 }
Jason Sams22534172009-08-04 16:58:20 -070087}
88
89
90