Seperate ProgramVertex from RenderScript.java and merge ProgramVertexAlloc into the ProgramVertex class.

This commit is contained in:
Jason Sams
2009-08-04 18:47:46 -07:00
parent a2136d6b18
commit 110195fe9f
9 changed files with 244 additions and 216 deletions

View File

@@ -16,18 +16,9 @@
package android.renderscript;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* @hide
*

View File

@@ -26,6 +26,8 @@ import android.util.Log;
*
**/
public class ProgramFragment extends BaseObj {
public static final int MAX_SLOT = 2;
public enum EnvMode {
REPLACE (0),
MODULATE (1),
@@ -48,17 +50,26 @@ public class ProgramFragment extends BaseObj {
mID = 0;
}
public void bindTexture(Allocation va, int slot) {
public void bindTexture(Allocation va, int slot)
throws IllegalArgumentException {
if((slot < 0) || (slot >= MAX_SLOT)) {
throw new IllegalArgumentException("Slot ID out of range.");
}
mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
}
public void bindSampler(Sampler vs, int slot) {
public void bindSampler(Sampler vs, int slot)
throws IllegalArgumentException {
if((slot < 0) || (slot >= MAX_SLOT)) {
throw new IllegalArgumentException("Slot ID out of range.");
}
mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
}
public static class Builder {
public static final int MAX_SLOT = 2;
RenderScript mRS;
Element mIn;
Element mOut;

View File

@@ -0,0 +1,190 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramVertex extends BaseObj {
public static final int MAX_LIGHT = 8;
ProgramVertex(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void destroy() {
mRS.nProgramVertexDestroy(mID);
mID = 0;
}
public void bindAllocation(int slot, MatrixAllocation va) {
mRS.nProgramVertexBindAllocation(mID, slot, va.mAlloc.mID);
}
public static class Builder {
RenderScript mRS;
Element mIn;
Element mOut;
Light[] mLights;
int mLightCount;
boolean mTextureMatrixEnable;
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
mIn = in;
mOut = out;
mLights = new Light[MAX_LIGHT];
mLightCount = 0;
}
public void setTextureMatrixEnable(boolean enable) {
mTextureMatrixEnable = enable;
}
public void addLight(Light l) throws IllegalStateException {
if(mLightCount >= MAX_LIGHT) {
throw new IllegalArgumentException("Max light count exceeded.");
}
mLights[mLightCount] = l;
mLightCount++;
}
static synchronized ProgramVertex internalCreate(RenderScript rs, Builder b) {
int inID = 0;
int outID = 0;
if (b.mIn != null) {
inID = b.mIn.mID;
}
if (b.mOut != null) {
outID = b.mOut.mID;
}
rs.nProgramVertexBegin(inID, outID);
for(int ct=0; ct < b.mLightCount; ct++) {
rs.nProgramVertexAddLight(b.mLights[ct].mID);
}
rs.nProgramVertexSetTextureMatrixEnable(b.mTextureMatrixEnable);
int id = rs.nProgramVertexCreate();
return new ProgramVertex(id, rs);
}
public ProgramVertex create() {
return internalCreate(mRS, this);
}
}
public static class MatrixAllocation {
static final int MODELVIEW_OFFSET = 0;
static final int PROJECTION_OFFSET = 16;
static final int TEXTURE_OFFSET = 32;
Matrix mModel;
Matrix mProjection;
Matrix mTexture;
public Allocation mAlloc;
public MatrixAllocation(RenderScript rs) {
mModel = new Matrix();
mProjection = new Matrix();
mTexture = new Matrix();
mAlloc = Allocation.createSized(rs, Element.USER_FLOAT, 48);
mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
}
public void destroy() {
mAlloc.destroy();
mAlloc = null;
}
public void loadModelview(Matrix m) {
mModel = m;
mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
}
public void loadProjection(Matrix m) {
mProjection = m;
mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
}
public void loadTexture(Matrix m) {
mTexture = m;
mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
}
public void setupOrthoWindow(int w, int h) {
mProjection.loadOrtho(0,w, h,0, -1,1);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupOrthoNormalized(int w, int h) {
// range -1,1 in the narrow axis.
if(w > h) {
float aspect = ((float)w) / h;
mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
} else {
float aspect = ((float)h) / w;
mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
}
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupProjectionNormalized(int w, int h) {
// range -1,1 in the narrow axis at z = 0.
Matrix m1 = new Matrix();
Matrix m2 = new Matrix();
if(w > h) {
float aspect = ((float)w) / h;
m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
} else {
float aspect = ((float)h) / w;
m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
}
m2.loadRotate(180, 0, 1, 0);
m1.loadMultiply(m1, m2);
m2.loadScale(-2, 2, 1);
m1.loadMultiply(m1, m2);
m2.loadTranslate(0, 0, 2);
m1.loadMultiply(m1, m2);
mProjection = m1;
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
}
}

View File

@@ -1,115 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.renderscript.Element;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramVertexAlloc {
public static final int MODELVIEW_OFFSET = 0;
public static final int PROJECTION_OFFSET = 16;
public static final int TEXTURE_OFFSET = 32;
Matrix mModel;
Matrix mProjection;
Matrix mTexture;
public Allocation mAlloc;
public ProgramVertexAlloc(RenderScript rs) {
mModel = new Matrix();
mProjection = new Matrix();
mTexture = new Matrix();
mAlloc = Allocation.createSized(rs, Element.USER_FLOAT, 48);
mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
}
public void loadModelview(Matrix m) {
mModel = m;
mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
}
public void loadProjection(Matrix m) {
mProjection = m;
mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
}
public void loadTexture(Matrix m) {
mTexture = m;
mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
}
public void setupOrthoWindow(int w, int h) {
mProjection.loadOrtho(0,w, h,0, -1,1);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupOrthoNormalized(int w, int h) {
// range -1,1 in the narrow axis.
if(w > h) {
float aspect = ((float)w) / h;
mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
} else {
float aspect = ((float)h) / w;
mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
}
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupProjectionNormalized(int w, int h) {
// range -1,1 in the narrow axis at z = 0.
Matrix m1 = new Matrix();
Matrix m2 = new Matrix();
if(w > h) {
float aspect = ((float)w) / h;
m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
} else {
float aspect = ((float)h) / w;
m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
}
m2.loadRotate(180, 0, 1, 0);
m1.loadMultiply(m1, m2);
m2.loadScale(-2, 2, 1);
m1.loadMultiply(m1, m2);
m2.loadTranslate(0, 0, 2);
m1.loadMultiply(m1, m2);
mProjection = m1;
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
}

View File

@@ -251,55 +251,6 @@ public class RenderScript {
return new TriangleMesh(id);
}
//////////////////////////////////////////////////////////////////////////////////
// ProgramVertex
public class ProgramVertex extends BaseObj {
ProgramVertex(int id) {
super(RenderScript.this);
mID = id;
}
public void destroy() {
nProgramVertexDestroy(mID);
mID = 0;
}
public void bindAllocation(int slot, Allocation va) {
nProgramVertexBindAllocation(mID, slot, va.mID);
}
}
public void programVertexBegin(Element in, Element out) {
int inID = 0;
int outID = 0;
if (in != null) {
inID = in.mID;
}
if (out != null) {
outID = out.mID;
}
nProgramVertexBegin(inID, outID);
}
public void programVertexSetType(int slot, Type t) {
nProgramVertexSetType(slot, t.mID);
}
public void programVertexSetTextureMatrixEnable(boolean enable) {
nProgramVertexSetTextureMatrixEnable(enable);
}
public void programVertexAddLight(Light l) {
nProgramVertexAddLight(l.mID);
}
public ProgramVertex programVertexCreate() {
int id = nProgramVertexCreate();
return new ProgramVertex(id);
}
//////////////////////////////////////////////////////////////////////////////////
// File

View File

@@ -24,7 +24,7 @@ import android.graphics.Bitmap;
import android.util.Log;
import android.renderscript.Matrix;
import android.renderscript.ProgramVertexAlloc;
import android.renderscript.ProgramVertex;
import android.renderscript.RenderScript;
import android.renderscript.Element;
import android.renderscript.Allocation;
@@ -81,9 +81,9 @@ public class FilmRS {
private ProgramStore mPSImages;
private ProgramFragment mPFBackground;
private ProgramFragment mPFImages;
private RenderScript.ProgramVertex mPVBackground;
private RenderScript.ProgramVertex mPVImages;
private ProgramVertexAlloc mPVA;
private ProgramVertex mPVBackground;
private ProgramVertex mPVImages;
private ProgramVertex.MatrixAllocation mPVA;
private Allocation mImages[];
private Allocation mAllocIDs;
@@ -144,14 +144,14 @@ public class FilmRS {
mLight = (new Light.Builder(mRS)).create();
mLight.setPosition(0, -0.5f, -1.0f);
mRS.programVertexBegin(null, null);
mRS.programVertexAddLight(mLight);
mPVBackground = mRS.programVertexCreate();
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
pvb.addLight(mLight);
mPVBackground = pvb.create();
mPVBackground.setName("PVBackground");
mRS.programVertexBegin(null, null);
mRS.programVertexSetTextureMatrixEnable(true);
mPVImages = mRS.programVertexCreate();
pvb = new ProgramVertex.Builder(mRS, null, null);
pvb.setTextureMatrixEnable(true);
mPVImages = pvb.create();
mPVImages.setName("PVImages");
}
@@ -239,9 +239,9 @@ public class FilmRS {
loadImages();
initState();
mPVA = new ProgramVertexAlloc(mRS);
mPVBackground.bindAllocation(0, mPVA.mAlloc);
mPVImages.bindAllocation(0, mPVA.mAlloc);
mPVA = new ProgramVertex.MatrixAllocation(mRS);
mPVBackground.bindAllocation(0, mPVA);
mPVImages.bindAllocation(0, mPVA);
mPVA.setupProjectionNormalized(320, 480);

View File

@@ -26,7 +26,7 @@ import android.graphics.drawable.Drawable;
import android.util.Log;
import android.renderscript.RenderScript;
import android.renderscript.ProgramVertexAlloc;
import android.renderscript.ProgramVertex;
import android.renderscript.Element;
import android.renderscript.Allocation;
import android.renderscript.Script;

View File

@@ -26,7 +26,7 @@ import android.renderscript.RenderScript;
import android.renderscript.ProgramFragment;
import android.renderscript.ProgramStore;
import android.renderscript.Allocation;
import android.renderscript.ProgramVertexAlloc;
import android.renderscript.ProgramVertex;
import static android.renderscript.Element.*;
import static android.util.MathUtils.*;
import android.graphics.Bitmap;
@@ -73,8 +73,8 @@ class GrassRS {
private Sampler mSampler;
private ProgramFragment mPfBackground;
private ProgramStore mPfsBackground;
private RenderScript.ProgramVertex mPvBackground;
private ProgramVertexAlloc mPvOrthoAlloc;
private ProgramVertex mPvBackground;
private ProgramVertex.MatrixAllocation mPvOrthoAlloc;
private Allocation mTexturesIDs;
private Allocation[] mTextures;
@@ -98,7 +98,7 @@ class GrassRS {
mResources = res;
initRS();
}
public void destroy() {
mScript.destroy();
mSampler.destroy();
@@ -259,13 +259,13 @@ class GrassRS {
}
private void createProgramVertex() {
mPvOrthoAlloc = new ProgramVertexAlloc(mRS);
mPvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
mRS.programVertexBegin(null, null);
mRS.programVertexSetTextureMatrixEnable(true);
mPvBackground = mRS.programVertexCreate();
mPvBackground.bindAllocation(0, mPvOrthoAlloc.mAlloc);
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
pvb.setTextureMatrixEnable(true);
mPvBackground = pvb.create();
mPvBackground.bindAllocation(0, mPvOrthoAlloc);
mPvBackground.setName("PVBackground");
}
}

View File

@@ -19,7 +19,7 @@ package com.android.rollo;
import java.io.Writer;
import android.renderscript.RenderScript;
import android.renderscript.ProgramVertexAlloc;
import android.renderscript.ProgramVertex;
import android.renderscript.Element;
import android.renderscript.Allocation;
import android.renderscript.Script;
@@ -102,10 +102,10 @@ public class RolloRS {
private ProgramStore mPSText;
private ProgramFragment mPFImages;
private ProgramFragment mPFText;
private RenderScript.ProgramVertex mPV;
private ProgramVertexAlloc mPVAlloc;
private RenderScript.ProgramVertex mPVOrtho;
private ProgramVertexAlloc mPVOrthoAlloc;
private ProgramVertex mPV;
private ProgramVertex.MatrixAllocation mPVAlloc;
private ProgramVertex mPVOrtho;
private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
private Allocation[] mIcons;
private Allocation[] mLabels;
@@ -162,22 +162,22 @@ public class RolloRS {
mPSText = bs.create();
mPSText.setName("PFSText");
mPVAlloc = new ProgramVertexAlloc(mRS);
mRS.programVertexBegin(null, null);
mRS.programVertexSetTextureMatrixEnable(false);
mPV = mRS.programVertexCreate();
mPV.setName("PV");
mPV.bindAllocation(0, mPVAlloc.mAlloc);
mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
mPVOrthoAlloc = new ProgramVertexAlloc(mRS);
mRS.programVertexBegin(null, null);
mRS.programVertexSetTextureMatrixEnable(true);
mPVOrtho = mRS.programVertexCreate();
mPVOrtho.setName("PVOrtho");
mPVOrtho.bindAllocation(0, mPVOrthoAlloc.mAlloc);
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
mPV = pvb.create();
mPV.setName("PV");
mPV.bindAllocation(0, mPVAlloc);
mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
pvb.setTextureMatrixEnable(true);
mPVOrtho = pvb.create();
mPVOrtho.setName("PVOrtho");
mPVOrtho.bindAllocation(0, mPVOrthoAlloc);
mRS.contextBindProgramVertex(mPV);
mAllocScratchBuf = new int[32];