Renderscript samples.

Resolving name conflict in model viewer.
Adding fov projection matrix function.
Adding helper methods for blending.

Change-Id: Ibd3f1510436811398787c285845d050158bd6de8
This commit is contained in:
Alex Sakhartchouk
2010-08-23 10:24:10 -07:00
parent 526e7a3c85
commit 32e09b5891
18 changed files with 764 additions and 24 deletions

View File

@ -79,7 +79,140 @@ public class ProgramStore extends BaseObj {
super(id, rs);
}
public static ProgramStore BlendNone_DepthTest(RenderScript rs) {
if(rs.mProgramStore_BlendNone_DepthTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendNone_DepthTest = builder.create();
}
return rs.mProgramStore_BlendNone_DepthTest;
}
public static ProgramStore BlendNone_DepthNoDepth(RenderScript rs) {
if(rs.mProgramStore_BlendNone_DepthNoDepth == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendNone_DepthNoDepth = builder.create();
}
return rs.mProgramStore_BlendNone_DepthNoDepth;
}
public static ProgramStore BlendNone_DepthNoTest(RenderScript rs) {
if(rs.mProgramStore_BlendNone_DepthNoTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendNone_DepthNoTest = builder.create();
}
return rs.mProgramStore_BlendNone_DepthNoTest;
}
public static ProgramStore BlendNone_DepthNoWrite(RenderScript rs) {
if(rs.mProgramStore_BlendNone_DepthNoWrite == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendNone_DepthNoWrite = builder.create();
}
return rs.mProgramStore_BlendNone_DepthNoWrite;
}
public static ProgramStore BlendAlpha_DepthTest(RenderScript rs) {
if(rs.mProgramStore_BlendAlpha_DepthTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendAlpha_DepthTest = builder.create();
}
return rs.mProgramStore_BlendAlpha_DepthTest;
}
public static ProgramStore BlendAlpha_DepthNoDepth(RenderScript rs) {
if(rs.mProgramStore_BlendAlpha_DepthNoDepth == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendAlpha_DepthNoDepth = builder.create();
}
return rs.mProgramStore_BlendAlpha_DepthNoDepth;
}
public static ProgramStore BlendAlpha_DepthNoTest(RenderScript rs) {
if(rs.mProgramStore_BlendAlpha_DepthNoTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendAlpha_DepthNoTest = builder.create();
}
return rs.mProgramStore_BlendAlpha_DepthNoTest;
}
public static ProgramStore BlendAlpha_DepthNoWrite(RenderScript rs) {
if(rs.mProgramStore_BlendAlpha_DepthNoWrite == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendAlpha_DepthNoWrite = builder.create();
}
return rs.mProgramStore_BlendAlpha_DepthNoWrite;
}
public static ProgramStore BlendAdd_DepthTest(RenderScript rs) {
if(rs.mProgramStore_BlendAdd_DepthTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.LESS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendAdd_DepthTest = builder.create();
}
return rs.mProgramStore_BlendAdd_DepthTest;
}
public static ProgramStore BlendAdd_DepthNoDepth(RenderScript rs) {
if(rs.mProgramStore_BlendAdd_DepthNoDepth == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendAdd_DepthNoDepth = builder.create();
}
return rs.mProgramStore_BlendAdd_DepthNoDepth;
}
public static ProgramStore BlendAdd_DepthNoTest(RenderScript rs) {
if(rs.mProgramStore_BlendAdd_DepthNoTest == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
builder.setDitherEnable(false);
builder.setDepthMask(true);
rs.mProgramStore_BlendAdd_DepthNoDepth = builder.create();
}
return rs.mProgramStore_BlendAdd_DepthNoTest;
}
public static ProgramStore BlendAdd_DepthNoWrite(RenderScript rs) {
if(rs.mProgramStore_BlendAdd_DepthNoWrite == null) {
ProgramStore.Builder builder = new ProgramStore.Builder(rs);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE);
builder.setDitherEnable(false);
builder.setDepthMask(false);
rs.mProgramStore_BlendAdd_DepthNoWrite = builder.create();
}
return rs.mProgramStore_BlendAdd_DepthNoWrite;
}
public static class Builder {
RenderScript mRS;
@ -109,8 +242,20 @@ public class ProgramStore extends BaseObj {
mColorMaskA = true;
mBlendSrc = BlendSrcFunc.ONE;
mBlendDst = BlendDstFunc.ZERO;
}
public Builder(RenderScript rs) {
mRS = rs;
mIn = null;
mOut = null;
mDepthFunc = DepthFunc.ALWAYS;
mDepthMask = false;
mColorMaskR = true;
mColorMaskG = true;
mColorMaskB = true;
mColorMaskA = true;
mBlendSrc = BlendSrcFunc.ONE;
mBlendDst = BlendDstFunc.ZERO;
}
public Builder setDepthFunc(DepthFunc func) {

View File

@ -46,6 +46,9 @@ public class ProgramVertex extends Program {
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
}
public Builder(RenderScript rs) {
mRS = rs;
}
public Builder setTextureMatrixEnable(boolean enable) {
mTextureMatrixEnable = enable;

View File

@ -549,6 +549,20 @@ public class RenderScript {
Sampler mSampler_WRAP_LINEAR;
Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
ProgramStore mProgramStore_BlendNone_DepthTest;
ProgramStore mProgramStore_BlendNone_DepthNoDepth;
ProgramStore mProgramStore_BlendNone_DepthNoTest;
ProgramStore mProgramStore_BlendNone_DepthNoWrite;
ProgramStore mProgramStore_BlendAlpha_DepthTest;
ProgramStore mProgramStore_BlendAlpha_DepthNoDepth;
ProgramStore mProgramStore_BlendAlpha_DepthNoTest;
ProgramStore mProgramStore_BlendAlpha_DepthNoWrite;
ProgramStore mProgramStore_BlendAdd_DepthTest;
ProgramStore mProgramStore_BlendAdd_DepthNoDepth;
ProgramStore mProgramStore_BlendAdd_DepthNoTest;
ProgramStore mProgramStore_BlendAdd_DepthNoWrite;
///////////////////////////////////////////////////////////////////////////////////
//

View File

@ -50,13 +50,6 @@ public class Sampler extends BaseObj {
super(id, rs);
}
Sampler mSampler_CLAMP_NEAREST;
Sampler mSampler_CLAMP_LINEAR;
Sampler mSampler_CLAMP_LINEAR_MIP;
Sampler mSampler_WRAP_NEAREST;
Sampler mSampler_WRAP_LINEAR;
Sampler mSampler_WRAP_LINEAR_MIP;
public static Sampler CLAMP_NEAREST(RenderScript rs) {
if(rs.mSampler_CLAMP_NEAREST == null) {
Builder b = new Builder(rs);

View File

@ -2,7 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.modelviewer">
<application android:label="ModelViewer">
<activity android:name="ModelViewer"
<activity android:name="SimpleModel"
android:label="SimpleModel"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>

View File

@ -90,7 +90,7 @@ public class SceneGraphRS {
}
private void initPFS() {
ProgramStore.Builder b = new ProgramStore.Builder(mRS, null, null);
ProgramStore.Builder b = new ProgramStore.Builder(mRS);
b.setDepthFunc(ProgramStore.DepthFunc.LESS);
b.setDitherEnable(false);
@ -118,7 +118,7 @@ public class SceneGraphRS {
}
private void initPV() {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS);
mPVBackground = pvb.create();
mPVA = new ProgramVertex.MatrixAllocation(mRS);

View File

@ -37,9 +37,9 @@ import android.widget.ListView;
import java.lang.Runtime;
public class ModelViewer extends Activity {
public class SimpleModel extends Activity {
private ModelViewerView mView;
private SimpleModelView mView;
@Override
public void onCreate(Bundle icicle) {
@ -47,7 +47,7 @@ public class ModelViewer extends Activity {
// Create our Preview view and set it as the content of our
// Activity
mView = new ModelViewerView(this);
mView = new SimpleModelView(this);
setContentView(mView);
}

View File

@ -24,7 +24,7 @@ import android.renderscript.ProgramStore.DepthFunc;
import android.util.Log;
public class ModelViewerRS {
public class SimpleModelRS {
private final int STATE_LAST_FOCUS = 1;
@ -32,7 +32,7 @@ public class ModelViewerRS {
int mHeight;
int mRotation;
public ModelViewerRS() {
public SimpleModelRS() {
}
public void init(RenderScriptGL rs, Resources res, int width, int height) {
@ -60,7 +60,7 @@ public class ModelViewerRS {
private Font mItalic;
private Allocation mTextAlloc;
private ScriptC_Modelviewer mScript;
private ScriptC_Simplemodel mScript;
int mLastX;
int mLastY;
@ -86,7 +86,7 @@ public class ModelViewerRS {
}
private void initPFS() {
ProgramStore.Builder b = new ProgramStore.Builder(mRS, null, null);
ProgramStore.Builder b = new ProgramStore.Builder(mRS);
b.setDepthFunc(ProgramStore.DepthFunc.LESS);
b.setDitherEnable(false);
@ -114,7 +114,7 @@ public class ModelViewerRS {
}
private void initPV() {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS);
mPVBackground = pvb.create();
mPVA = new ProgramVertex.MatrixAllocation(mRS);
@ -139,7 +139,7 @@ public class ModelViewerRS {
private void initRS() {
mScript = new ScriptC_Modelviewer(mRS, mRes, R.raw.modelviewer, true);
mScript = new ScriptC_Simplemodel(mRS, mRes, R.raw.simplemodel, true);
initPFS();
initPF();

View File

@ -39,15 +39,15 @@ import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class ModelViewerView extends RSSurfaceView {
public class SimpleModelView extends RSSurfaceView {
public ModelViewerView(Context context) {
public SimpleModelView(Context context) {
super(context);
//setFocusable(true);
}
private RenderScriptGL mRS;
private ModelViewerRS mRender;
private SimpleModelRS mRender;
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
@ -55,7 +55,7 @@ public class ModelViewerView extends RSSurfaceView {
if (mRS == null) {
mRS = createRenderScript(true);
mRS.contextSetSurface(w, h, holder.getSurface());
mRender = new ModelViewerRS();
mRender = new SimpleModelRS();
mRender.init(mRS, getResources(), w, h);
}
}

View File

@ -11,5 +11,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="RsRenderStates"
android:label="RsStates"
android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -0,0 +1,71 @@
/*
* 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 com.android.samples;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings.System;
import android.util.Config;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import java.lang.Runtime;
public class RsRenderStates extends Activity {
private RsRenderStatesView mView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create our Preview view and set it as the content of our
// Activity
mView = new RsRenderStatesView(this);
setContentView(mView);
}
@Override
protected void onResume() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onResume();
mView.onResume();
}
@Override
protected void onPause() {
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onPause();
mView.onPause();
}
}

View File

@ -0,0 +1,207 @@
/*
* 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 com.android.samples;
import java.io.Writer;
import android.content.res.Resources;
import android.renderscript.*;
import android.renderscript.ProgramStore.DepthFunc;
import android.util.Log;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class RsRenderStatesRS {
int mWidth;
int mHeight;
public RsRenderStatesRS() {
}
public void init(RenderScriptGL rs, Resources res, int width, int height) {
mRS = rs;
mRes = res;
mWidth = width;
mHeight = height;
mOptionsARGB.inScaled = false;
mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888;
mMode = 0;
mMaxModes = 4;
initRS();
}
private Resources mRes;
private RenderScriptGL mRS;
private Sampler mSampler;
private ProgramStore mProgStoreBlendNone;
private ProgramStore mProgStoreBlendAlpha;
private ProgramStore mProgStoreBlendAdd;
private ProgramFragment mProgFragmentTexture;
private ProgramFragment mProgFragmentColor;
private ProgramVertex mProgVertex;
private ProgramVertex.MatrixAllocation mPVA;
private Allocation mTexOpaque;
private Allocation mTexTransparent;
private Allocation mAllocPV;
private Mesh mMbyNMesh;
Font mFontSans;
Font mFontSerif;
Font mFontSerifBold;
Font mFontSerifItalic;
Font mFontSerifBoldItalic;
Font mFontMono;
private Allocation mTextAlloc;
private ScriptC_Rsrenderstates mScript;
private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
int mMode;
int mMaxModes;
public void onActionDown(int x, int y) {
mMode ++;
mMode = mMode % mMaxModes;
mScript.set_gDisplayMode(mMode);
}
private void initProgramStore() {
// Use stock the stock program store object
mProgStoreBlendNone = ProgramStore.BlendNone_DepthNoDepth(mRS);
// Create a custom program store
ProgramStore.Builder builder = new ProgramStore.Builder(mRS);
builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
builder.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
builder.setDitherEnable(false);
builder.setDepthMask(false);
mProgStoreBlendAlpha = builder.create();
mProgStoreBlendAdd = ProgramStore.BlendAdd_DepthNoDepth(mRS);
mScript.set_gProgStoreBlendNone(mProgStoreBlendNone);
mScript.set_gProgStoreBlendAlpha(mProgStoreBlendAlpha);
mScript.set_gProgStoreBlendAdd(mProgStoreBlendAdd);
}
private void initProgramFragment() {
Sampler.Builder bs = new Sampler.Builder(mRS);
bs.setMin(Sampler.Value.LINEAR);
bs.setMag(Sampler.Value.LINEAR);
bs.setWrapS(Sampler.Value.CLAMP);
bs.setWrapT(Sampler.Value.CLAMP);
mSampler = bs.create();
ProgramFragment.Builder texBuilder = new ProgramFragment.Builder(mRS);
texBuilder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
ProgramFragment.Builder.Format.RGBA, 0);
mProgFragmentTexture = texBuilder.create();
mProgFragmentTexture.bindSampler(mSampler, 0);
ProgramFragment.Builder colBuilder = new ProgramFragment.Builder(mRS);
colBuilder.setVaryingColor(false);
mProgFragmentColor = colBuilder.create();
mScript.set_gProgFragmentColor(mProgFragmentColor);
mScript.set_gProgFragmentTexture(mProgFragmentTexture);
}
private void initProgramVertex() {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS);
mProgVertex = pvb.create();
mPVA = new ProgramVertex.MatrixAllocation(mRS);
mProgVertex.bindAllocation(mPVA);
mPVA.setupOrthoWindow(mWidth, mHeight);
mScript.set_gProgVertex(mProgVertex);
}
private Allocation loadTextureRGB(int id) {
final Allocation allocation = Allocation.createFromBitmapResource(mRS, mRes,
id, Element.RGB_565(mRS), false);
allocation.uploadToTexture(0);
return allocation;
}
private Allocation loadTextureARGB(int id) {
Bitmap b = BitmapFactory.decodeResource(mRes, id, mOptionsARGB);
final Allocation allocation = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888(mRS), false);
allocation.uploadToTexture(0);
return allocation;
}
private void loadImages() {
mTexOpaque = loadTextureRGB(R.drawable.data);
mTexTransparent = loadTextureARGB(R.drawable.leaf);
mScript.set_gTexOpaque(mTexOpaque);
mScript.set_gTexTransparent(mTexTransparent);
}
private void initFonts() {
// Sans font by family name
mFontSans = Font.createFromFamily(mRS, mRes, "sans-serif", Font.Style.NORMAL, 8);
// Create font by file name
mFontSerif = Font.create(mRS, mRes, "DroidSerif-Regular.ttf", 8);
// Create fonts by family and style
mFontSerifBold = Font.createFromFamily(mRS, mRes, "serif", Font.Style.BOLD, 8);
mFontSerifItalic = Font.createFromFamily(mRS, mRes, "serif", Font.Style.ITALIC, 8);
mFontSerifBoldItalic = Font.createFromFamily(mRS, mRes, "serif", Font.Style.BOLD_ITALIC, 8);
mFontMono = Font.createFromFamily(mRS, mRes, "mono", Font.Style.NORMAL, 8);
mScript.set_gFontSans(mFontSans);
mScript.set_gFontSerif(mFontSerif);
mScript.set_gFontSerifBold(mFontSerifBold);
mScript.set_gFontSerifItalic(mFontSerifItalic);
mScript.set_gFontSerifBoldItalic(mFontSerifBoldItalic);
mScript.set_gFontMono(mFontMono);
}
private void initRS() {
mScript = new ScriptC_Rsrenderstates(mRS, mRes, R.raw.rsrenderstates, true);
initProgramStore();
initProgramFragment();
initProgramVertex();
initFonts();
loadImages();
mRS.contextBindRootScript(mScript);
}
}

View File

@ -0,0 +1,94 @@
/*
* 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 com.android.samples;
import java.io.Writer;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.renderscript.RenderScriptGL;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class RsRenderStatesView extends RSSurfaceView {
public RsRenderStatesView(Context context) {
super(context);
//setFocusable(true);
}
private RenderScriptGL mRS;
private RsRenderStatesRS mRender;
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
if (mRS == null) {
mRS = createRenderScript(true);
mRS.contextSetSurface(w, h, holder.getSurface());
mRender = new RsRenderStatesRS();
mRender.init(mRS, getResources(), w, h);
}
}
@Override
protected void onDetachedFromWindow() {
if(mRS != null) {
mRS = null;
destroyRenderScript();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// break point at here
// this method doesn't work when 'extends View' include 'extends ScrollView'.
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
boolean ret = false;
int act = ev.getAction();
if (act == ev.ACTION_DOWN) {
mRender.onActionDown((int)ev.getX(), (int)ev.getY());
ret = true;
}
return ret;
}
}

View File

@ -0,0 +1,194 @@
// 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.
#pragma version(1)
#pragma rs java_package_name(com.android.samples)
#include "rs_graphics.rsh"
rs_program_vertex gProgVertex;
rs_program_fragment gProgFragmentColor;
rs_program_fragment gProgFragmentTexture;
rs_program_store gProgStoreBlendNone;
rs_program_store gProgStoreBlendAlpha;
rs_program_store gProgStoreBlendAdd;
rs_allocation gTexOpaque;
rs_allocation gTexTransparent;
rs_mesh gMbyNMesh;
rs_font gFontSans;
rs_font gFontSerif;
rs_font gFontSerifBold;
rs_font gFontSerifItalic;
rs_font gFontSerifBoldItalic;
rs_font gFontMono;
int gDisplayMode;
#pragma rs export_var(gProgVertex, gProgFragmentColor, gProgFragmentTexture)
#pragma rs export_var(gProgStoreBlendNone, gProgStoreBlendAlpha, gProgStoreBlendAdd)
#pragma rs export_var(gTexOpaque, gTexTransparent)
#pragma rs export_var(gMbyNMesh)
#pragma rs export_var(gFontSans, gFontSerif, gFontSerifBold, gFontSerifItalic, gFontSerifBoldItalic, gFontMono)
//What we are showing
#pragma rs export_var(gDisplayMode)
void init() {
}
void displayFontSamples() {
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
int yPos = 30;
rsgBindFont(gFontSans);
rsgDrawText("Sans font sample", 30, yPos);
yPos += 30;
rsgFontColor(0.5f, 0.9f, 0.5f, 1.0f);
rsgBindFont(gFontSerif);
rsgDrawText("Serif font sample", 30, yPos);
yPos += 30;
rsgFontColor(0.7f, 0.7f, 0.7f, 1.0f);
rsgBindFont(gFontSerifBold);
rsgDrawText("Serif Bold font sample", 30, yPos);
yPos += 30;
rsgFontColor(0.5f, 0.5f, 0.9f, 1.0f);
rsgBindFont(gFontSerifItalic);
rsgDrawText("Serif Italic font sample", 30, yPos);
yPos += 30;
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
rsgBindFont(gFontSerifBoldItalic);
rsgDrawText("Serif Bold Italic font sample", 30, yPos);
yPos += 30;
rsgBindFont(gFontMono);
rsgDrawText("Monospace font sample", 30, yPos);
}
void displayShaderSamples() {
// Default vertex sahder
rsgBindProgramVertex(gProgVertex);
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsgProgramVertexLoadModelMatrix(&matrix);
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNone);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
rsgDrawQuadTexCoords(0, 0, 0, 0, 0,
0, 256, 0, 0, 1,
256, 256, 0, 1, 1,
256, 0, 0, 1, 0);
rsgDrawQuadTexCoords(200, 0, 0, 0, 0,
200, 128, 0, 0, 1,
328, 128, 0, 1, 1,
328, 0, 0, 1, 0);
rsgBindProgramStore(gProgStoreBlendAlpha);
rsgBindTexture(gProgFragmentTexture, 0, gTexTransparent);
rsgDrawQuadTexCoords(0, 200, 0, 0, 0,
0, 328, 0, 0, 1,
128, 328, 0, 1, 1,
128, 200, 0, 1, 0);
// Fragment program with simple color
rsgBindProgramFragment(gProgFragmentColor);
rsgProgramFragmentConstantColor(gProgFragmentColor, 0.9, 0.3, 0.3, 1);
rsgDrawRect(200, 300, 350, 450, 0);
rsgProgramFragmentConstantColor(gProgFragmentColor, 0.3, 0.9, 0.3, 1);
rsgDrawRect(50, 400, 400, 600, 0);
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
rsgBindFont(gFontMono);
rsgDrawText("Texture shader", 10, 50);
rsgDrawText("Alpha-blended texture shader", 10, 280);
rsgDrawText("Flat color shader", 100, 450);
}
void displayBlendingSamples() {
int i;
rsgBindProgramVertex(gProgVertex);
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsgProgramVertexLoadModelMatrix(&matrix);
rsgBindProgramFragment(gProgFragmentColor);
rsgBindProgramStore(gProgStoreBlendNone);
for(i = 0; i < 3; i ++) {
float iPlusOne = (float)(i + 1);
rsgProgramFragmentConstantColor(gProgFragmentColor,
0.1f*iPlusOne, 0.2f*iPlusOne, 0.3f*iPlusOne, 1);
float yPos = 150 * (float)i;
rsgDrawRect(0, yPos, 200, yPos + 200, 0);
}
rsgBindProgramStore(gProgStoreBlendAlpha);
for(i = 0; i < 3; i ++) {
float iPlusOne = (float)(i + 1);
rsgProgramFragmentConstantColor(gProgFragmentColor,
0.2f*iPlusOne, 0.3f*iPlusOne, 0.1f*iPlusOne, 0.5);
float yPos = 150 * (float)i;
rsgDrawRect(150, yPos, 350, yPos + 200, 0);
}
rsgBindProgramStore(gProgStoreBlendAdd);
for(i = 0; i < 3; i ++) {
float iPlusOne = (float)(i + 1);
rsgProgramFragmentConstantColor(gProgFragmentColor,
0.3f*iPlusOne, 0.1f*iPlusOne, 0.2f*iPlusOne, 0.5);
float yPos = 150 * (float)i;
rsgDrawRect(300, yPos, 500, yPos + 200, 0);
}
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
rsgBindFont(gFontMono);
rsgDrawText("No Blending", 10, 50);
rsgDrawText("Alpha Blending", 160, 150);
rsgDrawText("Additive Blending", 320, 250);
}
void displayMeshSamples() {
}
int root(int launchID) {
rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
rsgClearDepth(1.0f);
switch(gDisplayMode) {
case 0:
displayFontSamples();
break;
case 1:
displayShaderSamples();
break;
case 2:
displayBlendingSamples();
break;
case 3:
displayMeshSamples();
break;
}
return 10;
}

View File

@ -429,6 +429,15 @@ rsMatrixLoadFrustum(rs_matrix4x4 *m, float left, float right, float bottom, floa
m->m[15]= 0.f;
}
static void __attribute__((overloadable))
rsMatrixLoadPerspective(rs_matrix4x4* m, float fovy, float aspect, float near, float far) {
float top = near * tan((float) (fovy * M_PI / 360.0f));
float bottom = -top;
float left = bottom * aspect;
float right = top * aspect;
rsMatrixLoadFrustum(m, left, right, bottom, top, near, far);
}
static float4 __attribute__((overloadable))
rsMatrixMultiply(rs_matrix4x4 *m, float4 in) {
float4 ret;