1 /*
2  * Copyright (C) 2011 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 
17 #include "rsContext.h"
18 #include "rsProgramVertex.h"
19 #include "rsMatrix4x4.h"
20 
21 using namespace android;
22 using namespace android::renderscript;
23 
24 
ProgramVertex(Context * rsc,const char * shaderText,size_t shaderLength,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength,const uintptr_t * params,size_t paramLength)25 ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, size_t shaderLength,
26                              const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
27 
28                              const uintptr_t * params, size_t paramLength)
29     : Program(rsc, shaderText, shaderLength, params, paramLength) {
30     mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader, mUserShaderLen,
31                                  textureNames, textureNamesCount, textureNamesLength);
32 }
33 
~ProgramVertex()34 ProgramVertex::~ProgramVertex() {
35     mRSC->mHal.funcs.vertex.destroy(mRSC, this);
36 }
37 
setup(Context * rsc,ProgramVertexState * state)38 void ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
39     if ((state->mLast.get() == this) && !mDirty) {
40         return;
41     }
42 
43     if (!isUserProgram()) {
44         if (mHal.state.constants[0] == nullptr) {
45             rsc->setError(RS_ERROR_FATAL_UNKNOWN,
46                           "Unable to set fixed function emulation matrices because allocation is missing");
47             return;
48         }
49         float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
50                 rsc, mHal.state.constants[0]));
51         Matrix4x4 mvp;
52         mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
53         Matrix4x4 t;
54         t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
55         mvp.multiply(&t);
56         for (uint32_t i = 0; i < 16; i ++) {
57             f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
58         }
59         rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
60     }
61 
62     state->mLast.set(this);
63 
64     rsc->mHal.funcs.vertex.setActive(rsc, this);
65 }
66 
setProjectionMatrix(Context * rsc,const rsc_Matrix * m) const67 void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
68     if (isUserProgram()) {
69         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
70                       "Attempting to set fixed function emulation matrix projection on user program");
71         return;
72     }
73     if (mHal.state.constants[0] == nullptr) {
74         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
75                       "Unable to set fixed function emulation matrix projection because allocation is missing");
76         return;
77     }
78     float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
79                 rsc, mHal.state.constants[0]));
80     memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
81     mDirty = true;
82     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
83 }
84 
setModelviewMatrix(Context * rsc,const rsc_Matrix * m) const85 void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
86     if (isUserProgram()) {
87         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
88                       "Attempting to set fixed function emulation matrix modelview on user program");
89         return;
90     }
91     if (mHal.state.constants[0] == nullptr) {
92         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
93                       "Unable to set fixed function emulation matrix modelview because allocation is missing");
94         return;
95     }
96     float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
97                 rsc, mHal.state.constants[0]));
98     memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
99     mDirty = true;
100     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
101 }
102 
setTextureMatrix(Context * rsc,const rsc_Matrix * m) const103 void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
104     if (isUserProgram()) {
105         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
106                       "Attempting to set fixed function emulation matrix texture on user program");
107         return;
108     }
109     if (mHal.state.constants[0] == nullptr) {
110         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
111                       "Unable to set fixed function emulation matrix texture because allocation is missing");
112         return;
113     }
114     float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
115             rsc, mHal.state.constants[0]));
116     memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
117     mDirty = true;
118     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
119 }
120 
getProjectionMatrix(Context * rsc,rsc_Matrix * m) const121 void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
122     if (isUserProgram()) {
123         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
124                       "Attempting to get fixed function emulation matrix projection on user program");
125         return;
126     }
127     if (mHal.state.constants[0] == nullptr) {
128         rsc->setError(RS_ERROR_FATAL_UNKNOWN,
129                       "Unable to get fixed function emulation matrix projection because allocation is missing");
130         return;
131     }
132     float *f = static_cast<float *>(
133             rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
134     memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
135     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
136 }
137 
transformToScreen(Context * rsc,float * v4out,const float * v3in) const138 void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
139     if (isUserProgram()) {
140         return;
141     }
142     float *f = static_cast<float *>(
143             rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
144     Matrix4x4 mvp;
145     mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
146                      (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
147     mvp.vectorMultiply(v4out, v3in);
148     rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
149 }
150 
serialize(Context * rsc,OStream * stream) const151 void ProgramVertex::serialize(Context *rsc, OStream *stream) const {
152 }
153 
createFromStream(Context * rsc,IStream * stream)154 ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
155     return nullptr;
156 }
157 
158 
159 ///////////////////////////////////////////////////////////////////////
160 
ProgramVertexState()161 ProgramVertexState::ProgramVertexState() {
162 }
163 
~ProgramVertexState()164 ProgramVertexState::~ProgramVertexState() {
165 }
166 
init(Context * rsc)167 void ProgramVertexState::init(Context *rsc) {
168     ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
169                                                                  RS_KIND_USER, false, 1);
170     ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
171                                                              RS_KIND_USER, false, 2);
172     ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
173                                                              RS_KIND_USER, false, 3);
174     ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
175                                                              RS_KIND_USER, false, 4);
176 
177     const char *ebn1[] = { "MV", "P", "TexMatrix", "MVP" };
178     const Element *ebe1[] = {matrixElem.get(), matrixElem.get(),
179             matrixElem.get(), matrixElem.get()};
180     ObjectBaseRef<const Element> constInput = Element::create(rsc, 4, ebe1, ebn1);
181 
182     const char *ebn2[] = { "position", "color", "normal", "texture0" };
183     const Element *ebe2[] = {f4Elem.get(), f4Elem.get(), f3Elem.get(), f2Elem.get()};
184     ObjectBaseRef<const Element> attrElem = Element::create(rsc, 4, ebe2, ebn2);
185 
186     ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);
187 
188     const char *shaderString =
189             RS_SHADER_INTERNAL
190             "varying vec4 varColor;\n"
191             "varying vec2 varTex0;\n"
192             "void main() {\n"
193             "  gl_Position = UNI_MVP * ATTRIB_position;\n"
194             "  gl_PointSize = 1.0;\n"
195             "  varColor = ATTRIB_color;\n"
196             "  varTex0 = ATTRIB_texture0;\n"
197             "}\n";
198 
199     uintptr_t tmp[4];
200     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
201     tmp[1] = (uintptr_t)inputType.get();
202     tmp[2] = RS_PROGRAM_PARAM_INPUT;
203     tmp[3] = (uintptr_t)attrElem.get();
204 
205     ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
206                                           nullptr, 0, nullptr, tmp, 4);
207     Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
208                               RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
209     pv->bindAllocation(rsc, alloc, 0);
210 
211     mDefaultAlloc.set(alloc);
212     mDefault.set(pv);
213 
214     updateSize(rsc);
215 }
216 
updateSize(Context * rsc)217 void ProgramVertexState::updateSize(Context *rsc) {
218     float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(rsc, mDefaultAlloc.get()));
219 
220     float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
221     float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
222 
223     Matrix4x4 m;
224     m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
225     memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
226     memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
227 
228     m.loadIdentity();
229     memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
230     memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
231     rsc->mHal.funcs.allocation.unlock1D(rsc, mDefaultAlloc.get());
232 }
233 
deinit(Context * rsc)234 void ProgramVertexState::deinit(Context *rsc) {
235     mDefaultAlloc.clear();
236     mDefault.clear();
237     mLast.clear();
238 }
239 
240 
241 namespace android {
242 namespace renderscript {
243 
rsi_ProgramVertexCreate(Context * rsc,const char * shaderText,size_t shaderLength,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength,const uintptr_t * params,size_t paramLength)244 RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
245                                         const char** textureNames, size_t textureNamesCount,
246                                         const size_t *textureNamesLength,
247                                         const uintptr_t * params, size_t paramLength) {
248     ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
249                                           textureNames, textureNamesCount, textureNamesLength,
250                                           params, paramLength);
251     pv->incUserRef();
252     return pv;
253 }
254 
255 }
256 }
257