1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk * Copyright (C) 2011 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsContext.h"
18fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk#include "rsProgramVertex.h"
194edf030cbb7c6ac08dc563335c2af73c20f6e2e5Alex Sakhartchouk#include "rsMatrix4x4.h"
201aa5a4eb81b8b88aeb5d2b6f4c47356fd0a62923Jason Sams
21326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android;
22326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android::renderscript;
23326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
24326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
25748eb07e805b93c2bf79340d4937963ab739d17cAlex SakhartchoukProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, size_t shaderLength,
26748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                             const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
27748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk
28f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers                             const uintptr_t * params, size_t paramLength)
29afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    : Program(rsc, shaderText, shaderLength, params, paramLength) {
30f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader, mUserShaderLen,
31748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                 textureNames, textureNamesCount, textureNamesLength);
32326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
33326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
34afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertex::~ProgramVertex() {
35a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    mRSC->mHal.funcs.vertex.destroy(mRSC, this);
36c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams}
37c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
38c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchoukvoid ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
39c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    if ((state->mLast.get() == this) && !mDirty) {
40e64c2f172516972abe581ca1b762302dd2ba1199Jason Sams        return;
41c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    }
42c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
43afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!isUserProgram()) {
4444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        if (mHal.state.constants[0] == nullptr) {
4587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams            rsc->setError(RS_ERROR_FATAL_UNKNOWN,
4687319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                          "Unable to set fixed function emulation matrices because allocation is missing");
470bd3c8621aeaab8d616ebf7644f9da3cce4bbde8Alex Sakhartchouk            return;
480bd3c8621aeaab8d616ebf7644f9da3cce4bbde8Alex Sakhartchouk        }
49807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams        float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
50807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams                rsc, mHal.state.constants[0]));
5187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        Matrix4x4 mvp;
52e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
5387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        Matrix4x4 t;
54e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
55e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        mvp.multiply(&t);
56afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        for (uint32_t i = 0; i < 16; i ++) {
57e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk            f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
58e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        }
59807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams        rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
60c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    }
61c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
62c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    state->mLast.set(this);
63a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
64a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    rsc->mHal.funcs.vertex.setActive(rsc, this);
65c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams}
66c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
67afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
68afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
6987319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
7087319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix projection on user program");
71383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
72383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
7344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (mHal.state.constants[0] == nullptr) {
7487319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
7587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix projection because allocation is missing");
76e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
77e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
78807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
79807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams                rsc, mHal.state.constants[0]));
80c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
81cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
82807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
83c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
84c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
85afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
86afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
8787319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
8887319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix modelview on user program");
89383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
90383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
9144bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (mHal.state.constants[0] == nullptr) {
9287319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
9387319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix modelview because allocation is missing");
94e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
95e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
96807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
97807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams                rsc, mHal.state.constants[0]));
98c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
99cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
100807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
101c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
102c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
103afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
104afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
10587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
10687319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix texture on user program");
107383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
108383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
10944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (mHal.state.constants[0] == nullptr) {
11087319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
11187319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix texture because allocation is missing");
112e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
113e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
114807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
115807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            rsc, mHal.state.constants[0]));
116c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
117cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
118807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
119c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
120c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
121afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
122afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
12387319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
12487319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to get fixed function emulation matrix projection on user program");
125383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
126383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
12744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (mHal.state.constants[0] == nullptr) {
12887319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
12987319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to get fixed function emulation matrix projection because allocation is missing");
130e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
131e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
132807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(
133807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
13495333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk    memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
135807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
13695333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk}
13795333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk
138afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
139afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
140e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
141e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
142807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(
143807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams            rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
14487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    Matrix4x4 mvp;
14587fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
14687fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams                     (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
1473a97c59c6f8cc974d007080365b82c5177ba6a3fJason Sams    mvp.vectorMultiply(v4out, v3in);
148807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
1493a97c59c6f8cc974d007080365b82c5177ba6a3fJason Sams}
150326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
151e3150cfb3edb028407669e4a65e087eae77e718cJason Samsvoid ProgramVertex::serialize(Context *rsc, OStream *stream) const {
152fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
153fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
154afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
15544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    return nullptr;
156fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
157fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
1584815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams
159c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams///////////////////////////////////////////////////////////////////////
160c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
161afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertexState::ProgramVertexState() {
162326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
163326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
164afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertexState::~ProgramVertexState() {
165326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
166326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
167afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::init(Context *rsc) {
168c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
169c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                 RS_KIND_USER, false, 1);
170c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
171c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 2);
172c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
173c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 3);
174c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
175c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 4);
176c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
177f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    const char *ebn1[] = { "MV", "P", "TexMatrix", "MVP" };
178f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    const Element *ebe1[] = {matrixElem.get(), matrixElem.get(),
179f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams            matrixElem.get(), matrixElem.get()};
180f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    ObjectBaseRef<const Element> constInput = Element::create(rsc, 4, ebe1, ebn1);
181f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams
182f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    const char *ebn2[] = { "position", "color", "normal", "texture0" };
183f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    const Element *ebe2[] = {f4Elem.get(), f4Elem.get(), f3Elem.get(), f2Elem.get()};
184f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams    ObjectBaseRef<const Element> attrElem = Element::create(rsc, 4, ebe2, ebn2);
185c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
186c7968a0ac24f05d978616a79a5068b6b16dbbda6Jason Sams    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);
187e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
188a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams    const char *shaderString =
189a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            RS_SHADER_INTERNAL
190a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "varying vec4 varColor;\n"
191a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "varying vec2 varTex0;\n"
192a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "void main() {\n"
193a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "  gl_Position = UNI_MVP * ATTRIB_position;\n"
194a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "  gl_PointSize = 1.0;\n"
195a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "  varColor = ATTRIB_color;\n"
196a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "  varTex0 = ATTRIB_texture0;\n"
197a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams            "}\n";
198e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
199f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers    uintptr_t tmp[4];
200e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
201f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers    tmp[1] = (uintptr_t)inputType.get();
202e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    tmp[2] = RS_PROGRAM_PARAM_INPUT;
203f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers    tmp[3] = (uintptr_t)attrElem.get();
204e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
205a7f5e0406825151660c1c2e75c287e2fc8368023Jason Sams    ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
20644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes                                          nullptr, 0, nullptr, tmp, 4);
207c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
208eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
209383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    pv->bindAllocation(rsc, alloc, 0);
210fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
2118ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    mDefaultAlloc.set(alloc);
2128ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    mDefault.set(pv);
213cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
214771565f47fc44608444c00aa8fa3bda769ceaeceJason Sams    updateSize(rsc);
215e18844a5f648071fcc3594f84bca1728644af41aJason Sams}
216e18844a5f648071fcc3594f84bca1728644af41aJason Sams
217afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::updateSize(Context *rsc) {
218807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(rsc, mDefaultAlloc.get()));
219e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
220a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
221a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
222a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk
22387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    Matrix4x4 m;
224a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
225e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
226e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
2278ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams
2288ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    m.loadIdentity();
229e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
230e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
231807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    rsc->mHal.funcs.allocation.unlock1D(rsc, mDefaultAlloc.get());
2328ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams}
233326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
234afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::deinit(Context *rsc) {
235f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mDefaultAlloc.clear();
236f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mDefault.clear();
237f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mLast.clear();
238f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams}
239f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams
240326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
241326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
242326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
243326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
244748eb07e805b93c2bf79340d4937963ab739d17cAlex SakhartchoukRsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
245748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                        const char** textureNames, size_t textureNamesCount,
246748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                        const size_t *textureNamesLength,
247f8852d0494a260c583795a96a2a06c49b86a9b10Ian Rogers                                        const uintptr_t * params, size_t paramLength) {
248748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk    ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
249748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                          textureNames, textureNamesCount, textureNamesLength,
250748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                          params, paramLength);
2514815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams    pv->incUserRef();
2524815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams    return pv;
253b5909ce06dd10dcb5ac715572a05b2d225b77c98Jason Sams}
254326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
255326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
256326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
257