• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 uint32_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 uint32_t * params, size_t paramLength)
29     : Program(rsc, shaderText, shaderLength, params, paramLength) {
30     mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader.string(), mUserShader.length(),
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] == NULL) {
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] == NULL) {
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] == NULL) {
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] == NULL) {
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] == NULL) {
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 NULL;
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     Element::Builder constBuilder;
178     constBuilder.add(matrixElem.get(), "MV", 1);
179     constBuilder.add(matrixElem.get(), "P", 1);
180     constBuilder.add(matrixElem.get(), "TexMatrix", 1);
181     constBuilder.add(matrixElem.get(), "MVP", 1);
182     ObjectBaseRef<const Element> constInput = constBuilder.create(rsc);
183 
184     Element::Builder inputBuilder;
185     inputBuilder.add(f4Elem.get(), "position", 1);
186     inputBuilder.add(f4Elem.get(), "color", 1);
187     inputBuilder.add(f3Elem.get(), "normal", 1);
188     inputBuilder.add(f2Elem.get(), "texture0", 1);
189     ObjectBaseRef<const Element> attrElem = inputBuilder.create(rsc);
190 
191     ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);
192 
193     String8 shaderString(RS_SHADER_INTERNAL);
194     shaderString.append("varying vec4 varColor;\n");
195     shaderString.append("varying vec2 varTex0;\n");
196     shaderString.append("void main() {\n");
197     shaderString.append("  gl_Position = UNI_MVP * ATTRIB_position;\n");
198     shaderString.append("  gl_PointSize = 1.0;\n");
199     shaderString.append("  varColor = ATTRIB_color;\n");
200     shaderString.append("  varTex0 = ATTRIB_texture0;\n");
201     shaderString.append("}\n");
202 
203     uint32_t tmp[4];
204     tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
205     tmp[1] = (uint32_t)inputType.get();
206     tmp[2] = RS_PROGRAM_PARAM_INPUT;
207     tmp[3] = (uint32_t)attrElem.get();
208 
209     ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(), shaderString.length(),
210                                           NULL, 0, NULL, tmp, 4);
211     Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
212                               RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
213     pv->bindAllocation(rsc, alloc, 0);
214 
215     mDefaultAlloc.set(alloc);
216     mDefault.set(pv);
217 
218     updateSize(rsc);
219 }
220 
updateSize(Context * rsc)221 void ProgramVertexState::updateSize(Context *rsc) {
222     float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(rsc, mDefaultAlloc.get()));
223 
224     float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
225     float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
226 
227     Matrix4x4 m;
228     m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
229     memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
230     memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
231 
232     m.loadIdentity();
233     memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
234     memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
235     rsc->mHal.funcs.allocation.unlock1D(rsc, mDefaultAlloc.get());
236 }
237 
deinit(Context * rsc)238 void ProgramVertexState::deinit(Context *rsc) {
239     mDefaultAlloc.clear();
240     mDefault.clear();
241     mLast.clear();
242 }
243 
244 
245 namespace android {
246 namespace renderscript {
247 
rsi_ProgramVertexCreate(Context * rsc,const char * shaderText,size_t shaderLength,const char ** textureNames,size_t textureNamesCount,const size_t * textureNamesLength,const uint32_t * params,size_t paramLength)248 RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
249                                         const char** textureNames, size_t textureNamesCount,
250                                         const size_t *textureNamesLength,
251                                         const uint32_t * params, size_t paramLength) {
252     ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
253                                           textureNames, textureNamesCount, textureNamesLength,
254                                           params, paramLength);
255     pv->incUserRef();
256     return pv;
257 }
258 
259 }
260 }
261