rsProgramVertex.cpp revision 748eb07e805b93c2bf79340d4937963ab739d17c
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"
191aa5a4eb81b8b88aeb5d2b6f4c47356fd0a62923Jason Sams
20326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android;
21326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android::renderscript;
22326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
23326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
24748eb07e805b93c2bf79340d4937963ab739d17cAlex SakhartchoukProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, size_t shaderLength,
25748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                             const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
26748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk
27748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                             const uint32_t * params, size_t paramLength)
28afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    : Program(rsc, shaderText, shaderLength, params, paramLength) {
29748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk    mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader.string(), mUserShader.length(),
30748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                 textureNames, textureNamesCount, textureNamesLength);
31326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
32326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
33afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertex::~ProgramVertex() {
34a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    mRSC->mHal.funcs.vertex.destroy(mRSC, this);
35c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams}
36c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
37c19ff0177a7a0dadfc01b1990f822354fdc95991Alex Sakhartchoukvoid ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
38c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    if ((state->mLast.get() == this) && !mDirty) {
39e64c2f172516972abe581ca1b762302dd2ba1199Jason Sams        return;
40c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    }
41c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
42afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!isUserProgram()) {
43064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk        if (mHal.state.constants[0] == NULL) {
4487319de2b16a185cf360827c96a42cf1fcaae744Jason Sams            rsc->setError(RS_ERROR_FATAL_UNKNOWN,
4587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                          "Unable to set fixed function emulation matrices because allocation is missing");
460bd3c8621aeaab8d616ebf7644f9da3cce4bbde8Alex Sakhartchouk            return;
470bd3c8621aeaab8d616ebf7644f9da3cce4bbde8Alex Sakhartchouk        }
48a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk        float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
4987fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        Matrix4x4 mvp;
50e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
5187fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams        Matrix4x4 t;
52e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
53e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        mvp.multiply(&t);
54afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk        for (uint32_t i = 0; i < 16; i ++) {
55e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk            f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
56e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        }
57c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    }
58c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
59c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams    state->mLast.set(this);
60a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk
61a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    rsc->mHal.funcs.vertex.setActive(rsc, this);
62c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams}
63c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
64afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
65afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
6687319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
6787319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix projection on user program");
68383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
69383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
70064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    if (mHal.state.constants[0] == NULL) {
7187319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
7287319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix projection because allocation is missing");
73e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
74e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
75a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
76c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
77cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
78c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
79c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
80afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
81afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
8287319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
8387319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix modelview on user program");
84383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
85383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
86064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    if (mHal.state.constants[0] == NULL) {
8787319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
8887319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix modelview because allocation is missing");
89e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
90e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
91a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
92c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
93cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
94c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
95c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
96afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
97afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
9887319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
9987319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to set fixed function emulation matrix texture on user program");
100383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
101383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
102064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    if (mHal.state.constants[0] == NULL) {
10387319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
10487319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to set fixed function emulation matrix texture because allocation is missing");
105e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
106e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
107a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
108c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams    memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
109cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams    mDirty = true;
110c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams}
111c9d43db4d216b01b13aebfdb31d5615909591b33Jason Sams
112afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
113afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
11487319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
11587319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Attempting to get fixed function emulation matrix projection on user program");
116383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk        return;
117383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    }
118064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    if (mHal.state.constants[0] == NULL) {
11987319de2b16a185cf360827c96a42cf1fcaae744Jason Sams        rsc->setError(RS_ERROR_FATAL_UNKNOWN,
12087319de2b16a185cf360827c96a42cf1fcaae744Jason Sams                      "Unable to get fixed function emulation matrix projection because allocation is missing");
121e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
122e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
123a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
12495333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk    memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
12595333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk}
12695333f998fd4a983f89e5128e85f6f710d200bd9Alex Sakhartchouk
127afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
128afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (isUserProgram()) {
129e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk        return;
130e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    }
131a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk    float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
13287fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    Matrix4x4 mvp;
13387fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
13487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams                     (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
1353a97c59c6f8cc974d007080365b82c5177ba6a3fJason Sams    mvp.vectorMultiply(v4out, v3in);
1363a97c59c6f8cc974d007080365b82c5177ba6a3fJason Sams}
137326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
138afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertex::serialize(OStream *stream) const {
139fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
140fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
141afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
142fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    return NULL;
143fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk}
144fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
1454815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams
146c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams///////////////////////////////////////////////////////////////////////
147c460e55d78cbe8bee95c5c947dfe541218142a5bJason Sams
148afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertexState::ProgramVertexState() {
149326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
150326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
151afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukProgramVertexState::~ProgramVertexState() {
152326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
153326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
154afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::init(Context *rsc) {
155c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
156c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                                 RS_KIND_USER, false, 1);
157c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
158c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 2);
159c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
160c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 3);
161c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
162c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk                                                             RS_KIND_USER, false, 4);
163c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
164c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    Element::Builder constBuilder;
165c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    constBuilder.add(matrixElem.get(), "MV", 1);
166c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    constBuilder.add(matrixElem.get(), "P", 1);
167c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    constBuilder.add(matrixElem.get(), "TexMatrix", 1);
168c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    constBuilder.add(matrixElem.get(), "MVP", 1);
169c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> constInput = constBuilder.create(rsc);
170c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
171c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    Element::Builder inputBuilder;
172c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    inputBuilder.add(f4Elem.get(), "position", 1);
173c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    inputBuilder.add(f4Elem.get(), "color", 1);
174c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    inputBuilder.add(f3Elem.get(), "normal", 1);
175c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    inputBuilder.add(f2Elem.get(), "texture0", 1);
176c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<const Element> attrElem = inputBuilder.create(rsc);
177c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk
178c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);
179e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
180e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    String8 shaderString(RS_SHADER_INTERNAL);
181e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("varying vec4 varColor;\n");
1827ffcaf20cbb115326f3d72a983835d6c314a4cefAlex Sakhartchouk    shaderString.append("varying vec2 varTex0;\n");
183e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("void main() {\n");
184e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("  gl_Position = UNI_MVP * ATTRIB_position;\n");
185e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("  gl_PointSize = 1.0;\n");
186e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("  varColor = ATTRIB_color;\n");
187e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("  varTex0 = ATTRIB_texture0;\n");
188e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    shaderString.append("}\n");
189e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
19084e4027f83b20af59f5b1fc52be6e45f159d3970Alex Sakhartchouk    uint32_t tmp[4];
191e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
192c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    tmp[1] = (uint32_t)inputType.get();
193e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    tmp[2] = RS_PROGRAM_PARAM_INPUT;
194c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    tmp[3] = (uint32_t)attrElem.get();
195e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
196748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk    ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(), shaderString.length(),
197748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                          NULL, 0, NULL, tmp, 4);
198c700e649ca44d0dcff8b271e42d949ea72fe3c63Alex Sakhartchouk    Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
199eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
200383e5b1f68c321a77bfd7466fa1171a9bfab4a6fAlex Sakhartchouk    pv->bindAllocation(rsc, alloc, 0);
201fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk
2028ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    mDefaultAlloc.set(alloc);
2038ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    mDefault.set(pv);
204cfb1d11ce6826fce7241d316d8b7dcab661f63a6Jason Sams
205771565f47fc44608444c00aa8fa3bda769ceaeceJason Sams    updateSize(rsc);
206e18844a5f648071fcc3594f84bca1728644af41aJason Sams}
207e18844a5f648071fcc3594f84bca1728644af41aJason Sams
208afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::updateSize(Context *rsc) {
209e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    float *f = static_cast<float *>(mDefaultAlloc->getPtr());
210e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk
211a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
212a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
213a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk
21487fe59a2f4d4c74539bfa0bff5f9a7e320e99415Jason Sams    Matrix4x4 m;
215a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
216e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
217e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
2188ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams
2198ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    m.loadIdentity();
220e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
221e7ae69f4a70f1813cf8086ebd9714192c635300aAlex Sakhartchouk    memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
2228ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams}
223326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
224afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ProgramVertexState::deinit(Context *rsc) {
225f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mDefaultAlloc.clear();
226f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mDefault.clear();
227f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams    mLast.clear();
228f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams}
229f2649a961db2995e9e24a6c98f8a419f1496c1b7Jason Sams
230326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
231326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
232326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
233326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
234748eb07e805b93c2bf79340d4937963ab739d17cAlex SakhartchoukRsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
235748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                        const char** textureNames, size_t textureNamesCount,
236748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                        const size_t *textureNamesLength,
237748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                        const uint32_t * params, size_t paramLength) {
238748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk    ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
239748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                          textureNames, textureNamesCount, textureNamesLength,
240748eb07e805b93c2bf79340d4937963ab739d17cAlex Sakhartchouk                                          params, paramLength);
2414815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams    pv->incUserRef();
2424815c0d121310cfcd6a8aba4eab77a9910af53acJason Sams    return pv;
243b5909ce06dd10dcb5ac715572a05b2d225b77c98Jason Sams}
244326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
245326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
246326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
247