Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 543fbd26 authored by Alex Sakhartchouk's avatar Alex Sakhartchouk Committed by Android (Google) Code Review
Browse files

Merge "Streamlining texture creation. Adding render target textureas. Adding...

Merge "Streamlining texture creation. Adding render target textureas. Adding texture params for shaders. Making it easier to bind root script." into graphics-dev
parents 6dd7f5ea e8bb420a
Loading
Loading
Loading
Loading
+28 −6
Original line number Diff line number Diff line
@@ -93,23 +93,40 @@ public class FragmentShader extends Shader {
        return mProgram;
    }

    public void updateTextures() {
    ScriptField_ShaderParam_s getTextureParams() {
        RenderScriptGL rs = SceneManager.getRS();
        Resources res = SceneManager.getRes();
        if (rs == null || res == null) {
            return;
            return null;
        }

        ArrayList<ScriptField_ShaderParam_s.Item> paramList;
        paramList = new ArrayList<ScriptField_ShaderParam_s.Item>();

        int shaderTextureStart = mTextureTypes.size();
        for (int i = 0; i < mShaderTextureNames.size(); i ++) {
            ShaderParam sp = mSourceParams.get(mShaderTextureNames.get(i));
            if (sp != null && sp instanceof TextureParam) {
                TextureParam p = (TextureParam)sp;
                TextureBase tex = p.getTexture();
                if (tex != null) {
                    mProgram.bindTexture(tex.getRsData(), shaderTextureStart + i);
                ScriptField_ShaderParam_s.Item paramRS = new ScriptField_ShaderParam_s.Item();
                paramRS.bufferOffset = shaderTextureStart + i;
                paramRS.transformTimestamp = 0;
                paramRS.dataTimestamp = 0;
                paramRS.data = p.getRSData().getAllocation();
                paramList.add(paramRS);
            }
        }

        ScriptField_ShaderParam_s rsParams = null;
        int paramCount = paramList.size();
        if (paramCount != 0) {
            rsParams = new ScriptField_ShaderParam_s(rs, paramCount);
            for (int i = 0; i < paramCount; i++) {
                rsParams.set(paramList.get(i), i, false);
            }
            rsParams.copyAll();
        }
        return rsParams;
    }

    ScriptField_FragmentShader_s getRSData() {
@@ -126,6 +143,11 @@ public class FragmentShader extends Shader {
        ScriptField_FragmentShader_s.Item item = new ScriptField_FragmentShader_s.Item();
        item.program = mProgram;

        ScriptField_ShaderParam_s texParams = getTextureParams();
        if (texParams != null) {
            item.shaderTextureParams = texParams.getAllocation();
        }

        linkConstants(rs);
        if (mPerShaderConstants != null) {
            item.shaderConst = mConstantBuffer;
+10 −6
Original line number Diff line number Diff line
@@ -29,11 +29,11 @@ import android.content.res.Resources;
 */
public class RenderPass extends SceneGraphBase {

    Allocation mColorTarget;
    TextureRenderTarget mColorTarget;
    Float4 mClearColor;
    boolean mShouldClearColor;

    Allocation mDepthTarget;
    TextureRenderTarget mDepthTarget;
    float mClearDepth;
    boolean mShouldClearDepth;

@@ -59,7 +59,7 @@ public class RenderPass extends SceneGraphBase {
        mCamera = c;
    }

    public void setColorTarget(Allocation colorTarget) {
    public void setColorTarget(TextureRenderTarget colorTarget) {
        mColorTarget = colorTarget;
    }
    public void setClearColor(Float4 clearColor) {
@@ -69,7 +69,7 @@ public class RenderPass extends SceneGraphBase {
        mShouldClearColor = shouldClearColor;
    }

    public void setDepthTarget(Allocation depthTarget) {
    public void setDepthTarget(TextureRenderTarget depthTarget) {
        mDepthTarget = depthTarget;
    }
    public void setClearDepth(float clearDepth) {
@@ -89,8 +89,12 @@ public class RenderPass extends SceneGraphBase {
        }

        mRsField = new ScriptField_RenderPass_s.Item();
        mRsField.color_target = mColorTarget;
        mRsField.depth_target = mDepthTarget;
        if (mColorTarget != null) {
            mRsField.color_target = mColorTarget.getRsData(true).get_texture(0);
        }
        if (mColorTarget != null) {
            mRsField.depth_target = mDepthTarget.getRsData(true).get_texture(0);
        }
        mRsField.camera = mCamera != null ? mCamera.getRSData().getAllocation() : null;

        if (mObjectsToDraw.size() != 0) {
+3 −2
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ public class Renderable extends RenderableBase {
        }
    }

    void updateTextures(RenderScriptGL rs, Resources res) {
    void updateTextures(RenderScriptGL rs) {
        Iterator<ShaderParam> allParamsIter = mSourceParams.values().iterator();
        int paramIndex = 0;
        while (allParamsIter.hasNext()) {
@@ -137,7 +137,7 @@ public class Renderable extends RenderableBase {
                TextureParam p = (TextureParam)sp;
                TextureBase tex = p.getTexture();
                if (tex != null) {
                    mData.pf_textures[paramIndex++] = tex.getRsData();
                    mData.pf_textures[paramIndex++] = tex.getRsData(false).getAllocation();
                }
            }
        }
@@ -161,6 +161,7 @@ public class Renderable extends RenderableBase {
            return mField;
        }
        updateFieldItem(rs);
        updateTextures(rs);

        mField = new ScriptField_Renderable_s(rs, 1);
        mField.set(mData, 0, true);
+27 −53
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.scenegraph.SceneManager;
import com.android.scenegraph.TextureBase;

import android.content.res.Resources;
import android.os.AsyncTask;
@@ -37,38 +38,6 @@ import android.util.Log;
public class Scene extends SceneGraphBase {
    private static String TIMER_TAG = "TIMER";

    private class ImageLoaderTask extends AsyncTask<ArrayList<RenderableBase>, Void, Boolean> {
        protected Boolean doInBackground(ArrayList<RenderableBase>... objects) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < objects[0].size(); i ++) {
                Renderable dI = (Renderable)objects[0].get(i);
                dI.updateTextures(mRS, mRes);
            }
            long end = System.currentTimeMillis();
            Log.v(TIMER_TAG, "Texture init time: " + (end - start));
            return new Boolean(true);
        }

        protected void onPostExecute(Boolean result) {
        }
    }

    private class ShaderImageLoader extends AsyncTask<ArrayList<FragmentShader>, Void, Boolean> {
        protected Boolean doInBackground(ArrayList<FragmentShader>... objects) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < objects[0].size(); i ++) {
                FragmentShader sI = objects[0].get(i);
                sI.updateTextures();
            }
            long end = System.currentTimeMillis();
            Log.v(TIMER_TAG, "Shader texture init time: " + (end - start));
            return new Boolean(true);
        }

        protected void onPostExecute(Boolean result) {
        }
    }

    CompoundTransform mRootTransforms;
    HashMap<String, Transform> mTransformMap;
    ArrayList<RenderPass> mRenderPasses;
@@ -228,17 +197,13 @@ public class Scene extends SceneGraphBase {
    }

    public void initRenderPassRS(RenderScriptGL rs, SceneManager sceneManager) {
        new ShaderImageLoader().execute(mFragmentShaders);
        if (mRenderPasses.size() != 0) {
            mRenderPassAlloc = new ScriptField_RenderPass_s(mRS, mRenderPasses.size());
            for (int i = 0; i < mRenderPasses.size(); i ++) {
                mRenderPassAlloc.set(mRenderPasses.get(i).getRsField(mRS, mRes), i, false);
                new ImageLoaderTask().execute(mRenderPasses.get(i).getRenderables());
            }
            mRenderPassAlloc.copyAll();
            sceneManager.mRenderLoop.set_gRenderPasses(mRenderPassAlloc.getAllocation());
        } else {
            new ImageLoaderTask().execute(mRenderables);
        }
    }

@@ -279,9 +244,10 @@ public class Scene extends SceneGraphBase {
        sceneManager.mRenderLoop.set_gFragmentShaders(shaderData);
    }

    public void initRS(RenderScriptGL rs, Resources res, SceneManager sceneManager) {
        mRS = rs;
        mRes = res;
    public void initRS() {
        SceneManager sceneManager = SceneManager.getInstance();
        mRS = SceneManager.getRS();
        mRes = SceneManager.getRes();
        long start = System.currentTimeMillis();
        mTransformRSData = mRootTransforms.getRSData();
        long end = System.currentTimeMillis();
@@ -294,30 +260,38 @@ public class Scene extends SceneGraphBase {
        Log.v(TIMER_TAG, "Script init time: " + (end - start));

        start = System.currentTimeMillis();
        addDrawables(rs, res, sceneManager);
        addDrawables(mRS, mRes, sceneManager);
        end = System.currentTimeMillis();
        Log.v(TIMER_TAG, "Renderable init time: " + (end - start));

        addShaders(rs, res, sceneManager);
        addShaders(mRS, mRes, sceneManager);

        Allocation opaqueBuffer = Allocation.createSized(rs, Element.U32(rs), mRenderables.size());
        Allocation transparentBuffer = Allocation.createSized(rs,
                                                              Element.U32(rs), mRenderables.size());
        Allocation opaqueBuffer = null;
        if (mRenderables.size() > 0) {
            opaqueBuffer = Allocation.createSized(mRS, Element.U32(mRS), mRenderables.size());
        }
        Allocation transparentBuffer = null;
        if (mRenderables.size() > 0) {
            transparentBuffer = Allocation.createSized(mRS, Element.U32(mRS), mRenderables.size());
        }

        sceneManager.mRenderLoop.bind_gFrontToBack(opaqueBuffer);
        sceneManager.mRenderLoop.bind_gBackToFront(transparentBuffer);

        Allocation cameraData = Allocation.createSized(rs, Element.ALLOCATION(rs), mCameras.size());
        if (mCameras.size() > 0) {
            Allocation cameraData;
            cameraData = Allocation.createSized(mRS, Element.ALLOCATION(mRS), mCameras.size());
            Allocation[] cameraAllocs = new Allocation[mCameras.size()];
            for (int i = 0; i < mCameras.size(); i ++) {
                cameraAllocs[i] = mCameras.get(i).getRSData().getAllocation();
            }
            cameraData.copyFrom(cameraAllocs);
            sceneManager.mRenderLoop.set_gCameras(cameraData);
        }

        if (mLights.size() != 0) {
            Allocation lightData = Allocation.createSized(rs,
                                                          Element.ALLOCATION(rs),
        if (mLights.size() > 0) {
            Allocation lightData = Allocation.createSized(mRS,
                                                          Element.ALLOCATION(mRS),
                                                          mLights.size());
            Allocation[] lightAllocs = new Allocation[mLights.size()];
            for (int i = 0; i < mLights.size(); i ++) {
+47 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import android.renderscript.*;
import android.renderscript.Allocation.MipmapControl;
import android.renderscript.Mesh;
import android.renderscript.RenderScriptGL;
import android.renderscript.Type.Builder;
import android.util.Log;
import android.view.SurfaceHolder;

@@ -70,6 +71,52 @@ public class SceneManager extends SceneGraphBase {
    Scene mActiveScene;
    private static SceneManager sSceneManager;

    private Allocation sDefault2D;
    private Allocation sDefaultCube;

    private static Allocation getDefault(boolean isCube) {
        final int dimension = 4;
        final int bytesPerPixel = 4;
        int arraySize = dimension * dimension * bytesPerPixel;

        RenderScriptGL rs = sSceneManager.mRS;
        Type.Builder b = new Type.Builder(rs, Element.RGBA_8888(rs));
        b.setX(dimension).setY(dimension);
        if (isCube) {
            b.setFaces(true);
            arraySize *= 6;
        }
        Type bitmapType = b.create();

        Allocation.MipmapControl mip = Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
        int usage =  Allocation.USAGE_GRAPHICS_TEXTURE;
        Allocation defaultImage = Allocation.createTyped(rs, bitmapType, mip, usage);

        byte imageData[] = new byte[arraySize];
        defaultImage.copyFrom(imageData);
        return defaultImage;
    }

    static Allocation getDefaultTex2D() {
        if (sSceneManager == null) {
            return null;
        }
        if (sSceneManager.sDefault2D == null) {
            sSceneManager.sDefault2D = getDefault(false);
        }
        return sSceneManager.sDefault2D;
    }

    static Allocation getDefaultTexCube() {
        if (sSceneManager == null) {
            return null;
        }
        if (sSceneManager.sDefaultCube != null) {
            sSceneManager.sDefault2D = getDefault(true);
        }
        return sSceneManager.sDefaultCube;
    }

    public static boolean isSDCardPath(String path) {
        int sdCardIndex = path.indexOf("sdcard/");
        // We are looking for /sdcard/ or sdcard/
Loading