FileDocCategorySizeDatePackage
ShadersTestRS.javaAPI DocAndroid 5.1 API6886Thu Mar 12 22:22:44 GMT 2015com.android.shaderstest

ShadersTestRS

public class ShadersTestRS extends Object

Fields Summary
private android.content.res.Resources
mRes
private android.renderscript.RenderScriptGL
mRS
private android.renderscript.Sampler
mLinearClamp
private android.renderscript.Sampler
mNearestClamp
private android.renderscript.ProgramStore
mPSBackground
private android.renderscript.ProgramFragment
mPFBackground
private android.renderscript.ProgramVertex
mPVBackground
private ProgramVertexFixedFunction.Constants
mPVA
private android.renderscript.ProgramFragment
mPFVignette
private ScriptField_VignetteConstants_s
mFSVignetteConst
private android.renderscript.Allocation
mMeshTexture
private android.renderscript.Allocation
mScreen
private android.renderscript.Allocation
mScreenDepth
private ScriptField_MeshInfo
mMeshes
private ScriptC_shaderstest
mScript
Constructors Summary
public ShadersTestRS()

    
Methods Summary
public voidinit(android.renderscript.RenderScriptGL rs, android.content.res.Resources res)

        mRS = rs;
        mRes = res;
        initRS();
    
private voidinitBuffers(int width, int height)

        Builder b;
        b = new Builder(mRS, Element.RGBA_8888(mRS));
        b.setX(width).setY(height);
        mScreen = Allocation.createTyped(mRS, b.create(),
                Allocation.USAGE_GRAPHICS_TEXTURE | Allocation.USAGE_GRAPHICS_RENDER_TARGET);
        mScript.set_gScreen(mScreen);

        b = new Builder(mRS, Element.createPixel(mRS, DataType.UNSIGNED_16, DataKind.PIXEL_DEPTH));
        b.setX(width).setY(height);
        mScreenDepth = Allocation.createTyped(mRS, b.create(),
                Allocation.USAGE_GRAPHICS_RENDER_TARGET);
        mScript.set_gScreenDepth(mScreenDepth);
    
private voidinitMeshes(android.renderscript.FileA3D model)

        int numEntries = model.getIndexEntryCount();
        int numMeshes = 0;
        for (int i = 0; i < numEntries; i ++) {
            FileA3D.IndexEntry entry = model.getIndexEntry(i);
            if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
                numMeshes ++;
            }
        }

        if (numMeshes > 0) {
            mMeshes = new ScriptField_MeshInfo(mRS, numMeshes);

            for (int i = 0; i < numEntries; i ++) {
                FileA3D.IndexEntry entry = model.getIndexEntry(i);
                if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
                    Mesh mesh = entry.getMesh();
                    mMeshes.set_mMesh(i, mesh, false);
                    mMeshes.set_mNumIndexSets(i, mesh.getPrimitiveCount(), false);
                }
            }
            mMeshes.copyAll();
        } else {
            throw new RSRuntimeException("No valid meshes in file");
        }

        mScript.bind_gMeshes(mMeshes);
        mScript.invoke_updateMeshInfo();
    
private voidinitPF()

        mLinearClamp = Sampler.CLAMP_LINEAR(mRS);
        mScript.set_gLinear(mLinearClamp);

        mNearestClamp = Sampler.CLAMP_NEAREST(mRS);
        mScript.set_gNearest(mNearestClamp);

        ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS);
        b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
                     ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
        mPFBackground = b.create();
        mPFBackground.bindSampler(mLinearClamp, 0);
        mScript.set_gPFBackground(mPFBackground);

        mFSVignetteConst = new ScriptField_VignetteConstants_s(mRS, 1);
        mScript.bind_gFSVignetteConstants(mFSVignetteConst);

        ProgramFragment.Builder fs;

        fs = new ProgramFragment.Builder(mRS);
        fs.setShader(mRes, R.raw.vignette_fs);
        fs.addConstant(mFSVignetteConst.getAllocation().getType());
        fs.addTexture(Program.TextureType.TEXTURE_2D);
        mPFVignette = fs.create();
        mPFVignette.bindConstants(mFSVignetteConst.getAllocation(), 0);
        mScript.set_gPFVignette(mPFVignette);
    
private voidinitPFS()

        ProgramStore.Builder b = new ProgramStore.Builder(mRS);

        b.setDepthFunc(DepthFunc.LESS);
        b.setDitherEnabled(false);
        b.setDepthMaskEnabled(true);
        mPSBackground = b.create();

        mScript.set_gPFSBackground(mPSBackground);
    
private voidinitPV()

        ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
        mPVBackground = pvb.create();

        mPVA = new ProgramVertexFixedFunction.Constants(mRS);
        ((ProgramVertexFixedFunction) mPVBackground).bindConstants(mPVA);

        mScript.set_gPVBackground(mPVBackground);
    
private voidinitRS()

        mScript = new ScriptC_shaderstest(mRS, mRes, R.raw.shaderstest);

        initPFS();
        initPF();
        initPV();

        loadImage();

        initBuffers(1, 1);

        FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot);
        initMeshes(model);

        mRS.bindRootScript(mScript);
    
private voidloadImage()

        mMeshTexture = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
                Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                Allocation.USAGE_GRAPHICS_TEXTURE);
        mScript.set_gTMesh(mMeshTexture);
    
public voidonActionDown(float x, float y)

        mScript.invoke_onActionDown(x, y);
    
public voidonActionMove(float x, float y)

        mScript.invoke_onActionMove(x, y);
    
public voidonActionScale(float scale)

        mScript.invoke_onActionScale(scale);
    
public voidsurfaceChanged()

        initBuffers(mRS.getWidth(), mRS.getHeight());