| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import android.util.Config; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @hide |
| 26 | * |
| 27 | **/ |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 28 | public class ProgramVertex extends Program { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 29 | public static final int MAX_LIGHT = 8; |
| 30 | |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 31 | |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 32 | ProgramVertex(int id, RenderScript rs) { |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 33 | super(id, rs); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 36 | public void bindAllocation(MatrixAllocation va) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 37 | mRS.validate(); |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 38 | bindConstants(va.mAlloc, 0); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | |
| 42 | public static class Builder { |
| 43 | RenderScript mRS; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 44 | boolean mTextureMatrixEnable; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 45 | |
| 46 | public Builder(RenderScript rs, Element in, Element out) { |
| 47 | mRS = rs; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 50 | public Builder setTextureMatrixEnable(boolean enable) { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 51 | mTextureMatrixEnable = enable; |
| Jim Shuma | 288c871 | 2010-07-07 14:24:21 -0700 | [diff] [blame] | 52 | return this; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 55 | public ProgramVertex create() { |
| 56 | int id = mRS.nProgramVertexCreate(mTextureMatrixEnable); |
| 57 | return new ProgramVertex(id, mRS); |
| Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 58 | } |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 59 | } |
| Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 60 | |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 61 | public static class ShaderBuilder extends BaseProgramBuilder { |
| 62 | public ShaderBuilder(RenderScript rs) { |
| 63 | super(rs); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | public ProgramVertex create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 67 | mRS.validate(); |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 68 | int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2]; |
| 69 | int idx = 0; |
| 70 | |
| 71 | for (int i=0; i < mInputCount; i++) { |
| 72 | tmp[idx++] = 0; |
| 73 | tmp[idx++] = mInputs[i].mID; |
| 74 | } |
| 75 | for (int i=0; i < mOutputCount; i++) { |
| 76 | tmp[idx++] = 1; |
| 77 | tmp[idx++] = mOutputs[i].mID; |
| 78 | } |
| 79 | for (int i=0; i < mConstantCount; i++) { |
| 80 | tmp[idx++] = 2; |
| 81 | tmp[idx++] = mConstants[i].mID; |
| 82 | } |
| Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 83 | tmp[idx++] = 3; |
| 84 | tmp[idx++] = mTextureCount; |
| Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 85 | |
| 86 | int id = mRS.nProgramVertexCreate2(mShader, tmp); |
| 87 | ProgramVertex pv = new ProgramVertex(id, mRS); |
| 88 | initProgram(pv); |
| 89 | return pv; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | public static class MatrixAllocation { |
| 96 | static final int MODELVIEW_OFFSET = 0; |
| 97 | static final int PROJECTION_OFFSET = 16; |
| 98 | static final int TEXTURE_OFFSET = 32; |
| 99 | |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 100 | Matrix4f mModel; |
| 101 | Matrix4f mProjection; |
| 102 | Matrix4f mTexture; |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 103 | |
| 104 | public Allocation mAlloc; |
| 105 | |
| 106 | public MatrixAllocation(RenderScript rs) { |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 107 | mModel = new Matrix4f(); |
| 108 | mProjection = new Matrix4f(); |
| 109 | mTexture = new Matrix4f(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 110 | |
| Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 111 | mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 112 | mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat); |
| 113 | mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); |
| 114 | mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat); |
| 115 | } |
| 116 | |
| 117 | public void destroy() { |
| 118 | mAlloc.destroy(); |
| 119 | mAlloc = null; |
| 120 | } |
| 121 | |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 122 | public void loadModelview(Matrix4f m) { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 123 | mModel = m; |
| 124 | mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat); |
| 125 | } |
| 126 | |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 127 | public void loadProjection(Matrix4f m) { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 128 | mProjection = m; |
| 129 | mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat); |
| 130 | } |
| 131 | |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 132 | public void loadTexture(Matrix4f m) { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 133 | mTexture = m; |
| 134 | mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat); |
| 135 | } |
| 136 | |
| 137 | public void setupOrthoWindow(int w, int h) { |
| 138 | mProjection.loadOrtho(0,w, h,0, -1,1); |
| 139 | mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); |
| 140 | } |
| 141 | |
| 142 | public void setupOrthoNormalized(int w, int h) { |
| 143 | // range -1,1 in the narrow axis. |
| 144 | if(w > h) { |
| 145 | float aspect = ((float)w) / h; |
| 146 | mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1); |
| 147 | } else { |
| 148 | float aspect = ((float)h) / w; |
| 149 | mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1); |
| 150 | } |
| 151 | mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); |
| 152 | } |
| 153 | |
| 154 | public void setupProjectionNormalized(int w, int h) { |
| 155 | // range -1,1 in the narrow axis at z = 0. |
| Jason Sams | 25430d0 | 2010-02-02 15:26:40 -0800 | [diff] [blame] | 156 | Matrix4f m1 = new Matrix4f(); |
| 157 | Matrix4f m2 = new Matrix4f(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 158 | |
| 159 | if(w > h) { |
| 160 | float aspect = ((float)w) / h; |
| 161 | m1.loadFrustum(-aspect,aspect, -1,1, 1,100); |
| 162 | } else { |
| 163 | float aspect = ((float)h) / w; |
| 164 | m1.loadFrustum(-1,1, -aspect,aspect, 1,100); |
| 165 | } |
| 166 | |
| 167 | m2.loadRotate(180, 0, 1, 0); |
| 168 | m1.loadMultiply(m1, m2); |
| 169 | |
| 170 | m2.loadScale(-2, 2, 1); |
| 171 | m1.loadMultiply(m1, m2); |
| 172 | |
| 173 | m2.loadTranslate(0, 0, 2); |
| 174 | m1.loadMultiply(m1, m2); |
| 175 | |
| 176 | mProjection = m1; |
| 177 | mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |