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
2022534176fb5c1257130ef4ee589739ca42766a32Jason Samsimport android.util.Log;
2122534176fb5c1257130ef4ee589739ca42766a32Jason Sams
2222534176fb5c1257130ef4ee589739ca42766a32Jason Sams
239c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
24d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams * @deprecated in API 16
2511518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>The Renderscript fragment program, also known as fragment shader is responsible
2611518acc8c416023d8c2192b441a1767205676d9Robert Ly * for manipulating pixel data in a user defined way. It's constructed from a GLSL
2711518acc8c416023d8c2192b441a1767205676d9Robert Ly * shader string containing the program body, textures inputs, and a Type object
2811518acc8c416023d8c2192b441a1767205676d9Robert Ly * that describes the constants used by the program. Similar to the vertex programs,
2911518acc8c416023d8c2192b441a1767205676d9Robert Ly * when an allocation with constant input values is bound to the shader, its values
3011518acc8c416023d8c2192b441a1767205676d9Robert Ly * are sent to the graphics program automatically.</p>
3111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p> The values inside the allocation are not explicitly tracked. If they change between two draw
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * calls using the same program object, the runtime needs to be notified of that
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * change by calling rsgAllocationSyncAll so it could send the new values to hardware.
3411518acc8c416023d8c2192b441a1767205676d9Robert Ly * Communication between the vertex and fragment programs is handled internally in the
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * GLSL code. For example, if the fragment program is expecting a varying input called
3611518acc8c416023d8c2192b441a1767205676d9Robert Ly * varTex0, the GLSL code inside the program vertex must provide it.
3711518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
3822534176fb5c1257130ef4ee589739ca42766a32Jason Sams *
3922534176fb5c1257130ef4ee589739ca42766a32Jason Sams **/
407e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Samspublic class ProgramFragment extends Program {
4122534176fb5c1257130ef4ee589739ca42766a32Jason Sams    ProgramFragment(int id, RenderScript rs) {
427e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        super(id, rs);
4322534176fb5c1257130ef4ee589739ca42766a32Jason Sams    }
4422534176fb5c1257130ef4ee589739ca42766a32Jason Sams
45d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams    /**
46d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     * @deprecated in API 16
47d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams     */
48b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk    public static class Builder extends BaseProgramBuilder {
499c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
50d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
51df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Create a builder object.
52df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
53f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk         * @param rs Context to which the program will belong.
54df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
55b4d7bb6872f523b4318144202e119766ed9054edAlex Sakhartchouk        public Builder(RenderScript rs) {
567e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            super(rs);
577e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        }
587e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
599c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
60d4ca9910982853e50429ea9233f2150ca619b3d8Jason Sams         * @deprecated in API 16
61df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * Creates ProgramFragment from the current state of the builder
62df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         *
63df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         * @return  ProgramFragment
64df27202debdc2573b7882405010fba31ee4d46e6Alex Sakhartchouk         */
657e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        public ProgramFragment create() {
667e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            mRS.validate();
6767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + mTextureCount) * 2];
682123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            String[] texNames = new String[mTextureCount];
697e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            int idx = 0;
707e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
717e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mInputCount; i++) {
7267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.INPUT.mID;
73e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mInputs[i].getID(mRS);
747e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
757e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mOutputCount; i++) {
7667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.OUTPUT.mID;
77e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mOutputs[i].getID(mRS);
787e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
797e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            for (int i=0; i < mConstantCount; i++) {
8067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.CONSTANT.mID;
81e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                tmp[idx++] = mConstants[i].getID(mRS);
827e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            }
8367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            for (int i=0; i < mTextureCount; i++) {
8467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = ProgramParam.TEXTURE_TYPE.mID;
8567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk                tmp[idx++] = mTextureTypes[i].mID;
862123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk                texNames[i] = mTextureNames[i];
8767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            }
887e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams
892123b46ba85adb2cfb78068f8368e830640118d3Alex Sakhartchouk            int id = mRS.nProgramFragmentCreate(mShader, texNames, tmp);
907e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            ProgramFragment pf = new ProgramFragment(id, mRS);
917e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            initProgram(pf);
927e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams            return pf;
937e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams        }
947e5ab3b177b10fee304d011b3a4b9ee03e2b18b5Jason Sams    }
9522534176fb5c1257130ef4ee589739ca42766a32Jason Sams}
9622534176fb5c1257130ef4ee589739ca42766a32Jason Sams
9722534176fb5c1257130ef4ee589739ca42766a32Jason Sams
9822534176fb5c1257130ef4ee589739ca42766a32Jason Sams
99