122534176fb5c1257130ef4ee589739ca42766a32Jason Sams/*
222534176fb5c1257130ef4ee589739ca42766a32Jason Sams * Copyright (C) 2008 The Android Open Source Project
322534176fb5c1257130ef4ee589739ca42766a32Jason Sams *
422534176fb5c1257130ef4ee589739ca42766a32Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
522534176fb5c1257130ef4ee589739ca42766a32Jason Sams * you may not use this file except in compliance with the License.
622534176fb5c1257130ef4ee589739ca42766a32Jason Sams * You may obtain a copy of the License at
722534176fb5c1257130ef4ee589739ca42766a32Jason Sams *
822534176fb5c1257130ef4ee589739ca42766a32Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
922534176fb5c1257130ef4ee589739ca42766a32Jason Sams *
1022534176fb5c1257130ef4ee589739ca42766a32Jason Sams * Unless required by applicable law or agreed to in writing, software
1122534176fb5c1257130ef4ee589739ca42766a32Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1222534176fb5c1257130ef4ee589739ca42766a32Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1322534176fb5c1257130ef4ee589739ca42766a32Jason Sams * See the License for the specific language governing permissions and
1422534176fb5c1257130ef4ee589739ca42766a32Jason Sams * limitations under the License.
1522534176fb5c1257130ef4ee589739ca42766a32Jason Sams */
1622534176fb5c1257130ef4ee589739ca42766a32Jason Sams
1722534176fb5c1257130ef4ee589739ca42766a32Jason Samspackage android.renderscript;
1822534176fb5c1257130ef4ee589739ca42766a32Jason Sams
1922534176fb5c1257130ef4ee589739ca42766a32Jason Sams
209c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
21a90842283e322b1210e2bea5850b05177e3d87afTim Murray * @hide
22d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams * @deprecated in API 16
23c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p>The RenderScript fragment program, also known as fragment shader is responsible
2411518acc8c416023d8c2192b441a1767205676d9Robert Ly * for manipulating pixel data in a user defined way. It's constructed from a GLSL
2511518acc8c416023d8c2192b441a1767205676d9Robert Ly * shader string containing the program body, textures inputs, and a Type object
2611518acc8c416023d8c2192b441a1767205676d9Robert Ly * that describes the constants used by the program. Similar to the vertex programs,
2711518acc8c416023d8c2192b441a1767205676d9Robert Ly * when an allocation with constant input values is bound to the shader, its values
2811518acc8c416023d8c2192b441a1767205676d9Robert Ly * are sent to the graphics program automatically.</p>
2911518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p> The values inside the allocation are not explicitly tracked. If they change between two draw
3011518acc8c416023d8c2192b441a1767205676d9Robert Ly * calls using the same program object, the runtime needs to be notified of that
3111518acc8c416023d8c2192b441a1767205676d9Robert Ly * change by calling rsgAllocationSyncAll so it could send the new values to hardware.
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * Communication between the vertex and fragment programs is handled internally in the
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * GLSL code. For example, if the fragment program is expecting a varying input called
3411518acc8c416023d8c2192b441a1767205676d9Robert Ly * varTex0, the GLSL code inside the program vertex must provide it.
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
3622534176fb5c1257130ef4ee589739ca42766a32Jason Sams *
3722534176fb5c1257130ef4ee589739ca42766a32Jason Sams **/
387e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Samspublic class ProgramFragment extends Program {
39460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray    ProgramFragment(long id, RenderScript rs) {
407e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        super(id, rs);
4122534176fb5c1257130ef4ee589739ca42766a32Jason Sams    }
4222534176fb5c1257130ef4ee589739ca42766a32Jason Sams
43d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams    /**
44d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
45d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     */
46b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk    public static class Builder extends BaseProgramBuilder {
479c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
48d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
49df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Create a builder object.
50df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
51f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk         * @param rs Context to which the program will belong.
52df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
53b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder(RenderScript rs) {
547e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            super(rs);
557e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        }
567e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
579c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
58d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
59df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Creates ProgramFragment from the current state of the builder
60df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
61df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  ProgramFragment
62df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
637e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        public ProgramFragment create() {
647e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            mRS.validate();
659807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat            long[] tmp = new long[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
662123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            String[] texNames = new String[mTextureCount];
677e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            int idx = 0;
687e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
697e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mInputCount; i++) {
7067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.INPUT.mID;
719807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mInputs[i].getID(mRS);
727e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
737e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mOutputCount; i++) {
7467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.OUTPUT.mID;
759807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mOutputs[i].getID(mRS);
767e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
777e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mConstantCount; i++) {
7867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.CONSTANT.mID;
799807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mConstants[i].getID(mRS);
807e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
8167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            for (int i=0; i < mTextureCount; i++) {
8267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
839807155b11a25fb6068edc9b1cd82928ac2f05deAshok Bhat                tmp[idx++] = mTextureTypes[i].mID;
842123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                texNames[i] = mTextureNames[i];
8567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
867e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
87460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            long id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
887e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            ProgramFragment pf = new ProgramFragment(id, mRS);
897e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            initProgram(pf);
907e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            return pf;
917e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        }
927e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams    }
9322534176fb5c1257130ef4ee589739ca42766a32Jason Sams}
9422534176fb5c1257130ef4ee589739ca42766a32Jason Sams
9522534176fb5c1257130ef4ee589739ca42766a32Jason Sams
9622534176fb5c1257130ef4ee589739ca42766a32Jason Sams
97