blob: 51cb2a8ad49065c31bda42cc87fd3ccf4855e43d [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
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"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070018#include "rsProgramVertex.h"
Jason Sams4b962e52009-06-22 17:15:15 -070019
Jason Samsd19f10d2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
23
Jason Sams0011bcf2009-12-15 12:58:36 -080024ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText,
25 uint32_t shaderLength, const uint32_t * params,
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080026 uint32_t paramLength)
27 : Program(rsc, shaderText, shaderLength, params, paramLength) {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070028 mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader.string(), mUserShader.length());
Jason Samsd19f10d2009-05-22 14:03:28 -070029}
30
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080031ProgramVertex::~ProgramVertex() {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070032 mRSC->mHal.funcs.vertex.destroy(mRSC, this);
Jason Samsb37c0a52009-06-16 17:49:58 -070033}
34
Alex Sakhartchouk407cae92011-05-06 14:59:45 -070035void ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
Jason Samsbb51c402009-11-25 13:22:07 -080036 if ((state->mLast.get() == this) && !mDirty) {
Jason Sams7c1f4c32010-06-22 17:22:13 -070037 return;
Jason Samsbb51c402009-11-25 13:22:07 -080038 }
39
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080040 if (!isUserProgram()) {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070041 if (mHal.state.constants[0].get() == NULL) {
Jason Samsadd9d962010-11-22 16:20:16 -080042 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
43 "Unable to set fixed function emulation matrices because allocation is missing");
Alex Sakhartchouk7215b512010-09-27 10:29:47 -070044 return;
45 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070046 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Jason Samsfcf72312011-04-20 15:09:01 -070047 Matrix4x4 mvp;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070048 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Samsfcf72312011-04-20 15:09:01 -070049 Matrix4x4 t;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070050 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
51 mvp.multiply(&t);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080052 for (uint32_t i = 0; i < 16; i ++) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070053 f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
54 }
Jason Samsbb51c402009-11-25 13:22:07 -080055 }
56
57 state->mLast.set(this);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070058
59 rsc->mHal.funcs.vertex.setActive(rsc, this);
Jason Samsbb51c402009-11-25 13:22:07 -080060}
61
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080062void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
63 if (isUserProgram()) {
Jason Samsadd9d962010-11-22 16:20:16 -080064 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
65 "Attempting to set fixed function emulation matrix projection on user program");
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070066 return;
67 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070068 if (mHal.state.constants[0].get() == NULL) {
Jason Samsadd9d962010-11-22 16:20:16 -080069 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
70 "Unable to set fixed function emulation matrix projection because allocation is missing");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070071 return;
72 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070073 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -070074 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -070075 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -070076}
77
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080078void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
79 if (isUserProgram()) {
Jason Samsadd9d962010-11-22 16:20:16 -080080 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
81 "Attempting to set fixed function emulation matrix modelview on user program");
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070082 return;
83 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070084 if (mHal.state.constants[0].get() == NULL) {
Jason Samsadd9d962010-11-22 16:20:16 -080085 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
86 "Unable to set fixed function emulation matrix modelview because allocation is missing");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070087 return;
88 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070089 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -070090 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -070091 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -070092}
93
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080094void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
95 if (isUserProgram()) {
Jason Samsadd9d962010-11-22 16:20:16 -080096 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
97 "Attempting to set fixed function emulation matrix texture on user program");
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -070098 return;
99 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700100 if (mHal.state.constants[0].get() == NULL) {
Jason Samsadd9d962010-11-22 16:20:16 -0800101 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
102 "Unable to set fixed function emulation matrix texture because allocation is missing");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700103 return;
104 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700105 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -0700106 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -0700107 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700108}
109
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800110void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
111 if (isUserProgram()) {
Jason Samsadd9d962010-11-22 16:20:16 -0800112 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
113 "Attempting to get fixed function emulation matrix projection on user program");
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700114 return;
115 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700116 if (mHal.state.constants[0].get() == NULL) {
Jason Samsadd9d962010-11-22 16:20:16 -0800117 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
118 "Unable to get fixed function emulation matrix projection because allocation is missing");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700119 return;
120 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700121 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Alex Sakhartchoukcbed7522010-08-16 17:40:10 -0700122 memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
123}
124
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800125void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
126 if (isUserProgram()) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700127 return;
128 }
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700129 float *f = static_cast<float *>(mHal.state.constants[0]->getPtr());
Jason Samsfcf72312011-04-20 15:09:01 -0700130 Matrix4x4 mvp;
131 mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
132 (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Samse9ad9a72009-09-30 17:36:20 -0700133 mvp.vectorMultiply(v4out, v3in);
134}
Jason Samsd19f10d2009-05-22 14:03:28 -0700135
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800136void ProgramVertex::serialize(OStream *stream) const {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700137}
138
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800139ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700140 return NULL;
141}
142
Jason Sams0011bcf2009-12-15 12:58:36 -0800143
Jason Samsbb51c402009-11-25 13:22:07 -0800144///////////////////////////////////////////////////////////////////////
145
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800146ProgramVertexState::ProgramVertexState() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700147}
148
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800149ProgramVertexState::~ProgramVertexState() {
Jason Samsd19f10d2009-05-22 14:03:28 -0700150}
151
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800152void ProgramVertexState::init(Context *rsc) {
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700153 ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
154 RS_KIND_USER, false, 1);
155 ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
156 RS_KIND_USER, false, 2);
157 ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
158 RS_KIND_USER, false, 3);
159 ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
160 RS_KIND_USER, false, 4);
Jason Samsb6f2b132009-09-24 12:33:45 -0700161
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700162 Element::Builder constBuilder;
163 constBuilder.add(matrixElem.get(), "MV", 1);
164 constBuilder.add(matrixElem.get(), "P", 1);
165 constBuilder.add(matrixElem.get(), "TexMatrix", 1);
166 constBuilder.add(matrixElem.get(), "MVP", 1);
167 ObjectBaseRef<const Element> constInput = constBuilder.create(rsc);
Jason Samsb6f2b132009-09-24 12:33:45 -0700168
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700169 Element::Builder inputBuilder;
170 inputBuilder.add(f4Elem.get(), "position", 1);
171 inputBuilder.add(f4Elem.get(), "color", 1);
172 inputBuilder.add(f3Elem.get(), "normal", 1);
173 inputBuilder.add(f2Elem.get(), "texture0", 1);
174 ObjectBaseRef<const Element> attrElem = inputBuilder.create(rsc);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700175
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700176 ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700177
178 String8 shaderString(RS_SHADER_INTERNAL);
179 shaderString.append("varying vec4 varColor;\n");
Alex Sakhartchoukd2091632010-10-06 11:15:01 -0700180 shaderString.append("varying vec2 varTex0;\n");
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700181 shaderString.append("void main() {\n");
182 shaderString.append(" gl_Position = UNI_MVP * ATTRIB_position;\n");
183 shaderString.append(" gl_PointSize = 1.0;\n");
184 shaderString.append(" varColor = ATTRIB_color;\n");
185 shaderString.append(" varTex0 = ATTRIB_texture0;\n");
186 shaderString.append("}\n");
187
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800188 uint32_t tmp[4];
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700189 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700190 tmp[1] = (uint32_t)inputType.get();
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700191 tmp[2] = RS_PROGRAM_PARAM_INPUT;
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700192 tmp[3] = (uint32_t)attrElem.get();
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700193
194 ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(),
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800195 shaderString.length(), tmp, 4);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700196 Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
Jason Sams7e8aae72011-05-26 16:33:01 -0700197 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700198 pv->bindAllocation(rsc, alloc, 0);
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700199
Jason Sams9c54bdb2009-06-17 16:52:59 -0700200 mDefaultAlloc.set(alloc);
201 mDefault.set(pv);
Jason Sams9bee51c2009-08-05 13:57:03 -0700202
Jason Samsf603d212010-05-14 15:30:29 -0700203 updateSize(rsc);
Jason Samseb4b0312009-11-12 16:09:45 -0800204}
205
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800206void ProgramVertexState::updateSize(Context *rsc) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700207 float *f = static_cast<float *>(mDefaultAlloc->getPtr());
208
Jason Samsfcf72312011-04-20 15:09:01 -0700209 Matrix4x4 m;
Jason Samsf603d212010-05-14 15:30:29 -0700210 m.loadOrtho(0,rsc->getWidth(), rsc->getHeight(),0, -1,1);
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700211 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
212 memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
Jason Sams9c54bdb2009-06-17 16:52:59 -0700213
214 m.loadIdentity();
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700215 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
216 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
Jason Sams9c54bdb2009-06-17 16:52:59 -0700217}
Jason Samsd19f10d2009-05-22 14:03:28 -0700218
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800219void ProgramVertexState::deinit(Context *rsc) {
Jason Sams61f08d62009-09-25 16:37:33 -0700220 mDefaultAlloc.clear();
221 mDefault.clear();
Jason Sams61f08d62009-09-25 16:37:33 -0700222 mLast.clear();
Jason Sams61f08d62009-09-25 16:37:33 -0700223}
224
Jason Samsd19f10d2009-05-22 14:03:28 -0700225
226namespace android {
227namespace renderscript {
228
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700229RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700230 size_t shaderLength, const uint32_t * params,
231 size_t paramLength) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800232 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength);
233 pv->incUserRef();
234 return pv;
Jason Samsee411122009-07-21 12:20:54 -0700235}
Jason Samsd19f10d2009-05-22 14:03:28 -0700236
Jason Samsd19f10d2009-05-22 14:03:28 -0700237}
238}