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 **/ 28 public class ProgramVertex extends BaseObj { 29 public static final int MAX_LIGHT = 8; 30 ProgramVertex(int id, RenderScript rs)31 ProgramVertex(int id, RenderScript rs) { 32 super(rs); 33 mID = id; 34 } 35 bindAllocation(MatrixAllocation va)36 public void bindAllocation(MatrixAllocation va) { 37 mRS.nProgramVertexBindAllocation(mID, va.mAlloc.mID); 38 } 39 40 41 public static class Builder { 42 RenderScript mRS; 43 Element mIn; 44 Element mOut; 45 Light[] mLights; 46 int mLightCount; 47 boolean mTextureMatrixEnable; 48 49 Builder(RenderScript rs, Element in, Element out)50 public Builder(RenderScript rs, Element in, Element out) { 51 mRS = rs; 52 mIn = in; 53 mOut = out; 54 mLights = new Light[MAX_LIGHT]; 55 mLightCount = 0; 56 } 57 setTextureMatrixEnable(boolean enable)58 public void setTextureMatrixEnable(boolean enable) { 59 mTextureMatrixEnable = enable; 60 } 61 addLight(Light l)62 public void addLight(Light l) throws IllegalStateException { 63 if(mLightCount >= MAX_LIGHT) { 64 throw new IllegalArgumentException("Max light count exceeded."); 65 } 66 mLights[mLightCount] = l; 67 mLightCount++; 68 } 69 70 71 internalCreate(RenderScript rs, Builder b)72 static synchronized ProgramVertex internalCreate(RenderScript rs, Builder b) { 73 int inID = 0; 74 int outID = 0; 75 if (b.mIn != null) { 76 inID = b.mIn.mID; 77 } 78 if (b.mOut != null) { 79 outID = b.mOut.mID; 80 } 81 rs.nProgramVertexBegin(inID, outID); 82 for(int ct=0; ct < b.mLightCount; ct++) { 83 rs.nProgramVertexAddLight(b.mLights[ct].mID); 84 } 85 rs.nProgramVertexSetTextureMatrixEnable(b.mTextureMatrixEnable); 86 int id = rs.nProgramVertexCreate(); 87 return new ProgramVertex(id, rs); 88 } 89 create()90 public ProgramVertex create() { 91 return internalCreate(mRS, this); 92 } 93 } 94 95 96 97 public static class MatrixAllocation { 98 static final int MODELVIEW_OFFSET = 0; 99 static final int PROJECTION_OFFSET = 16; 100 static final int TEXTURE_OFFSET = 32; 101 102 Matrix mModel; 103 Matrix mProjection; 104 Matrix mTexture; 105 106 public Allocation mAlloc; 107 MatrixAllocation(RenderScript rs)108 public MatrixAllocation(RenderScript rs) { 109 mModel = new Matrix(); 110 mProjection = new Matrix(); 111 mTexture = new Matrix(); 112 113 mAlloc = Allocation.createSized(rs, Element.USER_F32(rs), 48); 114 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat); 115 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); 116 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat); 117 } 118 destroy()119 public void destroy() { 120 mAlloc.destroy(); 121 mAlloc = null; 122 } 123 loadModelview(Matrix m)124 public void loadModelview(Matrix m) { 125 mModel = m; 126 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat); 127 } 128 loadProjection(Matrix m)129 public void loadProjection(Matrix m) { 130 mProjection = m; 131 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat); 132 } 133 loadTexture(Matrix m)134 public void loadTexture(Matrix m) { 135 mTexture = m; 136 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat); 137 } 138 setupOrthoWindow(int w, int h)139 public void setupOrthoWindow(int w, int h) { 140 mProjection.loadOrtho(0,w, h,0, -1,1); 141 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); 142 } 143 setupOrthoNormalized(int w, int h)144 public void setupOrthoNormalized(int w, int h) { 145 // range -1,1 in the narrow axis. 146 if(w > h) { 147 float aspect = ((float)w) / h; 148 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1); 149 } else { 150 float aspect = ((float)h) / w; 151 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1); 152 } 153 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); 154 } 155 setupProjectionNormalized(int w, int h)156 public void setupProjectionNormalized(int w, int h) { 157 // range -1,1 in the narrow axis at z = 0. 158 Matrix m1 = new Matrix(); 159 Matrix m2 = new Matrix(); 160 161 if(w > h) { 162 float aspect = ((float)w) / h; 163 m1.loadFrustum(-aspect,aspect, -1,1, 1,100); 164 } else { 165 float aspect = ((float)h) / w; 166 m1.loadFrustum(-1,1, -aspect,aspect, 1,100); 167 } 168 169 m2.loadRotate(180, 0, 1, 0); 170 m1.loadMultiply(m1, m2); 171 172 m2.loadScale(-2, 2, 1); 173 m1.loadMultiply(m1, m2); 174 175 m2.loadTranslate(0, 0, 2); 176 m1.loadMultiply(m1, m2); 177 178 mProjection = m1; 179 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); 180 } 181 182 } 183 184 } 185 186