Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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" |
Alex Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 18 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES/glext.h> |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
Alex Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 23 | #endif //ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 24 | |
| 25 | #include "rsProgramVertex.h" |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 26 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
| 30 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 31 | ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, |
| 32 | uint32_t shaderLength, const uint32_t * params, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | uint32_t paramLength) |
| 34 | : Program(rsc, shaderText, shaderLength, params, paramLength) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 35 | init(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 38 | ProgramVertex::~ProgramVertex() { |
| 39 | if (mShaderID) { |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 40 | mRSC->mShaderCache.cleanupVertex(mShaderID); |
| 41 | } |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jason Sams | 5dad8b4 | 2009-12-15 19:10:11 -0800 | [diff] [blame] | 44 | void ProgramVertex::loadShader(Context *rsc) { |
| 45 | Program::loadShader(rsc, GL_VERTEX_SHADER); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 48 | void ProgramVertex::createShader(Context *rsc) { |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 49 | if (mUserShader.length() > 1) { |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 50 | |
Alex Sakhartchouk | 8442e0b | 2010-08-31 12:02:01 -0700 | [diff] [blame] | 51 | appendUserConstants(); |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 52 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 53 | for (uint32_t ct=0; ct < mInputCount; ct++) { |
| 54 | const Element *e = mInputElements[ct].get(); |
| 55 | for (uint32_t field=0; field < e->getFieldCount(); field++) { |
| 56 | const Element *f = e->getField(field); |
Jason Sams | 6d1cf41 | 2010-06-17 18:05:38 -0700 | [diff] [blame] | 57 | const char *fn = e->getFieldName(field); |
| 58 | |
| 59 | if (fn[0] == '#') { |
| 60 | continue; |
| 61 | } |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 62 | |
| 63 | // Cannot be complex |
| 64 | rsAssert(!f->getFieldCount()); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | switch (f->getComponent().getVectorSize()) { |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 66 | case 1: mShader.append("attribute float ATTRIB_"); break; |
| 67 | case 2: mShader.append("attribute vec2 ATTRIB_"); break; |
| 68 | case 3: mShader.append("attribute vec3 ATTRIB_"); break; |
| 69 | case 4: mShader.append("attribute vec4 ATTRIB_"); break; |
| 70 | default: |
| 71 | rsAssert(0); |
| 72 | } |
| 73 | |
Jason Sams | 6d1cf41 | 2010-06-17 18:05:38 -0700 | [diff] [blame] | 74 | mShader.append(fn); |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 75 | mShader.append(";\n"); |
| 76 | } |
| 77 | } |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 78 | mShader.append(mUserShader); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 79 | } else { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 80 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 81 | "ProgramFragment::createShader cannot create program, shader code not defined"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 82 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 85 | void ProgramVertex::setupGL2(Context *rsc, ProgramVertexState *state, ShaderCache *sc) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 86 | if ((state->mLast.get() == this) && !mDirty) { |
Jason Sams | 7c1f4c3 | 2010-06-22 17:22:13 -0700 | [diff] [blame] | 87 | return; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 90 | rsc->checkError("ProgramVertex::setupGL2 start"); |
Jason Sams | ce9adcc | 2009-12-16 14:13:06 -0800 | [diff] [blame] | 91 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 92 | if (!isUserProgram()) { |
| 93 | if (mConstants[0].get() == NULL) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 94 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 95 | "Unable to set fixed function emulation matrices because allocation is missing"); |
Alex Sakhartchouk | 7215b51 | 2010-09-27 10:29:47 -0700 | [diff] [blame] | 96 | return; |
| 97 | } |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 98 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
| 99 | Matrix mvp; |
| 100 | mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 101 | Matrix t; |
| 102 | t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
| 103 | mvp.multiply(&t); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 104 | for (uint32_t i = 0; i < 16; i ++) { |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 105 | f[RS_PROGRAM_VERTEX_MVP_OFFSET + i] = mvp.m[i]; |
| 106 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 109 | rsc->checkError("ProgramVertex::setupGL2 begin uniforms"); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 110 | setupUserConstants(rsc, sc, false); |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 111 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 112 | state->mLast.set(this); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 113 | rsc->checkError("ProgramVertex::setupGL2"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 116 | void ProgramVertex::setProjectionMatrix(Context *rsc, const rsc_Matrix *m) const { |
| 117 | if (isUserProgram()) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 118 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 119 | "Attempting to set fixed function emulation matrix projection on user program"); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 122 | if (mConstants[0].get() == NULL) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 123 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 124 | "Unable to set fixed function emulation matrix projection because allocation is missing"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 127 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 128 | memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 129 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 132 | void ProgramVertex::setModelviewMatrix(Context *rsc, const rsc_Matrix *m) const { |
| 133 | if (isUserProgram()) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 134 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 135 | "Attempting to set fixed function emulation matrix modelview on user program"); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | if (mConstants[0].get() == NULL) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 139 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 140 | "Unable to set fixed function emulation matrix modelview because allocation is missing"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 141 | return; |
| 142 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 143 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 144 | memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 145 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 148 | void ProgramVertex::setTextureMatrix(Context *rsc, const rsc_Matrix *m) const { |
| 149 | if (isUserProgram()) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 150 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 151 | "Attempting to set fixed function emulation matrix texture on user program"); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 154 | if (mConstants[0].get() == NULL) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 155 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 156 | "Unable to set fixed function emulation matrix texture because allocation is missing"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 159 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 160 | memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 161 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 164 | void ProgramVertex::getProjectionMatrix(Context *rsc, rsc_Matrix *m) const { |
| 165 | if (isUserProgram()) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 166 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 167 | "Attempting to get fixed function emulation matrix projection on user program"); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 168 | return; |
| 169 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 170 | if (mConstants[0].get() == NULL) { |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 171 | rsc->setError(RS_ERROR_FATAL_UNKNOWN, |
| 172 | "Unable to get fixed function emulation matrix projection because allocation is missing"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
Alex Sakhartchouk | cbed752 | 2010-08-16 17:40:10 -0700 | [diff] [blame] | 175 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
| 176 | memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix)); |
| 177 | } |
| 178 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | void ProgramVertex::transformToScreen(Context *rsc, float *v4out, const float *v3in) const { |
| 180 | if (isUserProgram()) { |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 183 | float *f = static_cast<float *>(mConstants[0]->getPtr()); |
Jason Sams | e9ad9a7 | 2009-09-30 17:36:20 -0700 | [diff] [blame] | 184 | Matrix mvp; |
| 185 | mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], |
| 186 | (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 187 | mvp.vectorMultiply(v4out, v3in); |
| 188 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 189 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 190 | void ProgramVertex::init(Context *rsc) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 191 | uint32_t attribCount = 0; |
| 192 | uint32_t uniformCount = 0; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 193 | if (mUserShader.size() > 0) { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 194 | for (uint32_t ct=0; ct < mInputCount; ct++) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 195 | initAddUserElement(mInputElements[ct].get(), mAttribNames, NULL, &attribCount, RS_SHADER_ATTR); |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 196 | } |
Jason Sams | e17e365 | 2010-01-13 14:52:46 -0800 | [diff] [blame] | 197 | for (uint32_t ct=0; ct < mConstantCount; ct++) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 198 | initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformCount, RS_SHADER_UNI); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 199 | } |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 200 | } |
Jason Sams | add9d96 | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 201 | createShader(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 204 | void ProgramVertex::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 207 | ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 208 | return NULL; |
| 209 | } |
| 210 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 211 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 212 | /////////////////////////////////////////////////////////////////////// |
| 213 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 214 | ProgramVertexState::ProgramVertexState() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 217 | ProgramVertexState::~ProgramVertexState() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 220 | void ProgramVertexState::init(Context *rsc) { |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 221 | const Element *matrixElem = Element::create(rsc, RS_TYPE_MATRIX_4X4, RS_KIND_USER, false, 1); |
Alex Sakhartchouk | d209163 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 222 | const Element *f2Elem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 223 | const Element *f3Elem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3); |
| 224 | const Element *f4Elem = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4); |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 225 | |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 226 | rsc->mStateElement.elementBuilderBegin(); |
| 227 | rsc->mStateElement.elementBuilderAdd(matrixElem, "MV", 1); |
| 228 | rsc->mStateElement.elementBuilderAdd(matrixElem, "P", 1); |
| 229 | rsc->mStateElement.elementBuilderAdd(matrixElem, "TexMatrix", 1); |
| 230 | rsc->mStateElement.elementBuilderAdd(matrixElem, "MVP", 1); |
| 231 | const Element *constInput = rsc->mStateElement.elementBuilderCreate(rsc); |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 232 | |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 233 | rsc->mStateElement.elementBuilderBegin(); |
| 234 | rsc->mStateElement.elementBuilderAdd(f4Elem, "position", 1); |
| 235 | rsc->mStateElement.elementBuilderAdd(f4Elem, "color", 1); |
| 236 | rsc->mStateElement.elementBuilderAdd(f3Elem, "normal", 1); |
Alex Sakhartchouk | d209163 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 237 | rsc->mStateElement.elementBuilderAdd(f2Elem, "texture0", 1); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 238 | const Element *attrElem = rsc->mStateElement.elementBuilderCreate(rsc); |
| 239 | |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 240 | Type *inputType = Type::getType(rsc, constInput, 1, 0, 0, false, false); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 241 | |
| 242 | String8 shaderString(RS_SHADER_INTERNAL); |
| 243 | shaderString.append("varying vec4 varColor;\n"); |
Alex Sakhartchouk | d209163 | 2010-10-06 11:15:01 -0700 | [diff] [blame] | 244 | shaderString.append("varying vec2 varTex0;\n"); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 245 | shaderString.append("void main() {\n"); |
| 246 | shaderString.append(" gl_Position = UNI_MVP * ATTRIB_position;\n"); |
| 247 | shaderString.append(" gl_PointSize = 1.0;\n"); |
| 248 | shaderString.append(" varColor = ATTRIB_color;\n"); |
| 249 | shaderString.append(" varTex0 = ATTRIB_texture0;\n"); |
| 250 | shaderString.append("}\n"); |
| 251 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 252 | uint32_t tmp[4]; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 253 | tmp[0] = RS_PROGRAM_PARAM_CONSTANT; |
| 254 | tmp[1] = (uint32_t)inputType; |
| 255 | tmp[2] = RS_PROGRAM_PARAM_INPUT; |
| 256 | tmp[3] = (uint32_t)attrElem; |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 257 | |
| 258 | ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(), |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 259 | shaderString.length(), tmp, 4); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 260 | Allocation *alloc = new Allocation(rsc, inputType, RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 261 | pv->bindAllocation(rsc, alloc, 0); |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 262 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 263 | mDefaultAlloc.set(alloc); |
| 264 | mDefault.set(pv); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 265 | |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 266 | updateSize(rsc); |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 269 | void ProgramVertexState::updateSize(Context *rsc) { |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 270 | float *f = static_cast<float *>(mDefaultAlloc->getPtr()); |
| 271 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 272 | Matrix m; |
Jason Sams | f603d21 | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 273 | m.loadOrtho(0,rsc->getWidth(), rsc->getHeight(),0, -1,1); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 274 | memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m.m, sizeof(m)); |
| 275 | memcpy(&f[RS_PROGRAM_VERTEX_MVP_OFFSET], m.m, sizeof(m)); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 276 | |
| 277 | m.loadIdentity(); |
Alex Sakhartchouk | c984dd7 | 2010-09-14 09:50:43 -0700 | [diff] [blame] | 278 | memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m.m, sizeof(m)); |
| 279 | memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m.m, sizeof(m)); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 280 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 281 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 282 | void ProgramVertexState::deinit(Context *rsc) { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 283 | mDefaultAlloc.clear(); |
| 284 | mDefault.clear(); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 285 | mLast.clear(); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 288 | |
| 289 | namespace android { |
| 290 | namespace renderscript { |
| 291 | |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 292 | RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, const char * shaderText, |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 293 | uint32_t shaderLength, const uint32_t * params, |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 294 | uint32_t paramLength) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 295 | ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength); |
| 296 | pv->incUserRef(); |
| 297 | return pv; |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 298 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 299 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 300 | } |
| 301 | } |