blob: 3492f635c1243b7875adddced6b9d7ef80c39a51 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams326e0dd2009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070018#include "rsProgramVertex.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080019#include "rsMatrix4x4.h"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070020
Jason Sams326e0dd2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
24
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080025ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, size_t shaderLength,
26 const char** textureNames, size_t textureNamesCount, const size_t *textureNamesLength,
27
Ian Rogersf8852d02014-01-29 15:35:17 -080028 const uintptr_t * params, size_t paramLength)
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029 : Program(rsc, shaderText, shaderLength, params, paramLength) {
Jason Samsf313dc32013-07-09 14:29:39 -070030 mRSC->mHal.funcs.vertex.init(mRSC, this, mUserShader, mUserShaderLen,
Alex Sakhartchouk748eb072012-02-15 16:21:46 -080031 textureNames, textureNamesCount, textureNamesLength);
Jason Sams326e0dd2009-05-22 14:03:28 -070032}
33
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034ProgramVertex::~ProgramVertex() {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070035 mRSC->mHal.funcs.vertex.destroy(mRSC, this);
Jason Sams56bc1af2009-06-16 17:49:58 -070036}
37
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -070038void ProgramVertex::setup(Context *rsc, ProgramVertexState *state) {
Jason Samsc460e552009-11-25 13:22:07 -080039 if ((state->mLast.get() == this) && !mDirty) {
Jason Samse64c2f12010-06-22 17:22:13 -070040 return;
Jason Samsc460e552009-11-25 13:22:07 -080041 }
42
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080043 if (!isUserProgram()) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070044 if (mHal.state.constants[0] == nullptr) {
Jason Sams87319de2010-11-22 16:20:16 -080045 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
46 "Unable to set fixed function emulation matrices because allocation is missing");
Alex Sakhartchouk0bd3c862010-09-27 10:29:47 -070047 return;
48 }
Jason Sams807fdc42012-07-25 17:55:39 -070049 float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
50 rsc, mHal.state.constants[0]));
Jason Sams87fe59a2011-04-20 15:09:01 -070051 Matrix4x4 mvp;
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070052 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams87fe59a2011-04-20 15:09:01 -070053 Matrix4x4 t;
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070054 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
55 mvp.multiply(&t);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080056 for (uint32_t i = 0; i < 16; i ++) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070057 f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i];
58 }
Jason Sams807fdc42012-07-25 17:55:39 -070059 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Samsc460e552009-11-25 13:22:07 -080060 }
61
62 state->mLast.set(this);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070063
64 rsc->mHal.funcs.vertex.setActive(rsc, this);
Jason Samsc460e552009-11-25 13:22:07 -080065}
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const {
68 if (isUserProgram()) {
Jason Sams87319de2010-11-22 16:20:16 -080069 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
70 "Attempting to set fixed function emulation matrix projection on user program");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070071 return;
72 }
Chris Wailes44bef6f2014-08-12 13:51:10 -070073 if (mHal.state.constants[0] == nullptr) {
Jason Sams87319de2010-11-22 16:20:16 -080074 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
75 "Unable to set fixed function emulation matrix projection because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070076 return;
77 }
Jason Sams807fdc42012-07-25 17:55:39 -070078 float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
79 rsc, mHal.state.constants[0]));
Jason Samsc9d43db2009-07-28 12:02:16 -070080 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -070081 mDirty = true;
Jason Sams807fdc42012-07-25 17:55:39 -070082 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Samsc9d43db2009-07-28 12:02:16 -070083}
84
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const {
86 if (isUserProgram()) {
Jason Sams87319de2010-11-22 16:20:16 -080087 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
88 "Attempting to set fixed function emulation matrix modelview on user program");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070089 return;
90 }
Chris Wailes44bef6f2014-08-12 13:51:10 -070091 if (mHal.state.constants[0] == nullptr) {
Jason Sams87319de2010-11-22 16:20:16 -080092 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
93 "Unable to set fixed function emulation matrix modelview because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -070094 return;
95 }
Jason Sams807fdc42012-07-25 17:55:39 -070096 float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
97 rsc, mHal.state.constants[0]));
Jason Samsc9d43db2009-07-28 12:02:16 -070098 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -070099 mDirty = true;
Jason Sams807fdc42012-07-25 17:55:39 -0700100 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Samsc9d43db2009-07-28 12:02:16 -0700101}
102
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800103void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const {
104 if (isUserProgram()) {
Jason Sams87319de2010-11-22 16:20:16 -0800105 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
106 "Attempting to set fixed function emulation matrix texture on user program");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700107 return;
108 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700109 if (mHal.state.constants[0] == nullptr) {
Jason Sams87319de2010-11-22 16:20:16 -0800110 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
111 "Unable to set fixed function emulation matrix texture because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700112 return;
113 }
Jason Sams807fdc42012-07-25 17:55:39 -0700114 float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(
115 rsc, mHal.state.constants[0]));
Jason Samsc9d43db2009-07-28 12:02:16 -0700116 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700117 mDirty = true;
Jason Sams807fdc42012-07-25 17:55:39 -0700118 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Samsc9d43db2009-07-28 12:02:16 -0700119}
120
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800121void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const {
122 if (isUserProgram()) {
Jason Sams87319de2010-11-22 16:20:16 -0800123 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
124 "Attempting to get fixed function emulation matrix projection on user program");
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700125 return;
126 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700127 if (mHal.state.constants[0] == nullptr) {
Jason Sams87319de2010-11-22 16:20:16 -0800128 rsc->setError(RS_ERROR_FATAL_UNKNOWN,
129 "Unable to get fixed function emulation matrix projection because allocation is missing");
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700130 return;
131 }
Jason Sams807fdc42012-07-25 17:55:39 -0700132 float *f = static_cast<float *>(
133 rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700134 memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix));
Jason Sams807fdc42012-07-25 17:55:39 -0700135 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const {
139 if (isUserProgram()) {
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700140 return;
141 }
Jason Sams807fdc42012-07-25 17:55:39 -0700142 float *f = static_cast<float *>(
143 rsc->mHal.funcs.allocation.lock1D(rsc, mHal.state.constants[0]));
Jason Sams87fe59a2011-04-20 15:09:01 -0700144 Matrix4x4 mvp;
145 mvp.loadMultiply((Matrix4x4 *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
146 (Matrix4x4 *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams3a97c592009-09-30 17:36:20 -0700147 mvp.vectorMultiply(v4out, v3in);
Jason Sams807fdc42012-07-25 17:55:39 -0700148 rsc->mHal.funcs.allocation.unlock1D(rsc, mHal.state.constants[0]);
Jason Sams3a97c592009-09-30 17:36:20 -0700149}
Jason Sams326e0dd2009-05-22 14:03:28 -0700150
Jason Samse3150cf2012-07-24 18:10:20 -0700151void ProgramVertex::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700155 return nullptr;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700156}
157
Jason Sams4815c0d2009-12-15 12:58:36 -0800158
Jason Samsc460e552009-11-25 13:22:07 -0800159///////////////////////////////////////////////////////////////////////
160
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800161ProgramVertexState::ProgramVertexState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700162}
163
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800164ProgramVertexState::~ProgramVertexState() {
Jason Sams326e0dd2009-05-22 14:03:28 -0700165}
166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167void ProgramVertexState::init(Context *rsc) {
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700168 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);
Jason Sams8287c0c2009-09-24 12:33:45 -0700176
Jason Samsf313dc32013-07-09 14:29:39 -0700177 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);
Jason Sams8287c0c2009-09-24 12:33:45 -0700181
Jason Samsf313dc32013-07-09 14:29:39 -0700182 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);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700185
Jason Samsc7968a02014-11-11 16:24:36 -0800186 ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700187
Jason Samsa7f5e042013-07-08 16:46:18 -0700188 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";
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700198
Ian Rogersf8852d02014-01-29 15:35:17 -0800199 uintptr_t tmp[4];
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700200 tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
Ian Rogersf8852d02014-01-29 15:35:17 -0800201 tmp[1] = (uintptr_t)inputType.get();
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700202 tmp[2] = RS_PROGRAM_PARAM_INPUT;
Ian Rogersf8852d02014-01-29 15:35:17 -0800203 tmp[3] = (uintptr_t)attrElem.get();
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700204
Jason Samsa7f5e042013-07-08 16:46:18 -0700205 ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
Chris Wailes44bef6f2014-08-12 13:51:10 -0700206 nullptr, 0, nullptr, tmp, 4);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700207 Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
Jason Samseb4fe182011-05-26 16:33:01 -0700208 RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700209 pv->bindAllocation(rsc, alloc, 0);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700210
Jason Sams8ce125b2009-06-17 16:52:59 -0700211 mDefaultAlloc.set(alloc);
212 mDefault.set(pv);
Jason Samscfb1d112009-08-05 13:57:03 -0700213
Jason Sams771565f2010-05-14 15:30:29 -0700214 updateSize(rsc);
Jason Samse18844a2009-11-12 16:09:45 -0800215}
216
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800217void ProgramVertexState::updateSize(Context *rsc) {
Jason Sams807fdc42012-07-25 17:55:39 -0700218 float *f = static_cast<float *>(rsc->mHal.funcs.allocation.lock1D(rsc, mDefaultAlloc.get()));
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700219
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800220 float surfaceWidth = (float)rsc->getCurrentSurfaceWidth();
221 float surfaceHeight = (float)rsc->getCurrentSurfaceHeight();
222
Jason Sams87fe59a2011-04-20 15:09:01 -0700223 Matrix4x4 m;
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800224 m.loadOrtho(0, surfaceWidth, surfaceHeight, 0, -1, 1);
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700225 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m));
226 memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m));
Jason Sams8ce125b2009-06-17 16:52:59 -0700227
228 m.loadIdentity();
Alex Sakhartchouke7ae69f2010-09-14 09:50:43 -0700229 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m));
230 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m));
Jason Sams807fdc42012-07-25 17:55:39 -0700231 rsc->mHal.funcs.allocation.unlock1D(rsc, mDefaultAlloc.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700232}
Jason Sams326e0dd2009-05-22 14:03:28 -0700233
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800234void ProgramVertexState::deinit(Context *rsc) {
Jason Samsf2649a92009-09-25 16:37:33 -0700235 mDefaultAlloc.clear();
236 mDefault.clear();
Jason Samsf2649a92009-09-25 16:37:33 -0700237 mLast.clear();
Jason Samsf2649a92009-09-25 16:37:33 -0700238}
239
Jason Sams326e0dd2009-05-22 14:03:28 -0700240
241namespace android {
242namespace renderscript {
243
Alex Sakhartchouk748eb072012-02-15 16:21:46 -0800244RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, size_t shaderLength,
245 const char** textureNames, size_t textureNamesCount,
246 const size_t *textureNamesLength,
Ian Rogersf8852d02014-01-29 15:35:17 -0800247 const uintptr_t * params, size_t paramLength) {
Alex Sakhartchouk748eb072012-02-15 16:21:46 -0800248 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength,
249 textureNames, textureNamesCount, textureNamesLength,
250 params, paramLength);
Jason Sams4815c0d2009-12-15 12:58:36 -0800251 pv->incUserRef();
252 return pv;
Jason Samsb5909ce2009-07-21 12:20:54 -0700253}
Jason Sams326e0dd2009-05-22 14:03:28 -0700254
Jason Sams326e0dd2009-05-22 14:03:28 -0700255}
256}