如何在Android中与OpenGL共享Renderscript分配

如何在Android中与OpenGL共享Renderscript分配,android,opengl-es-2.0,renderscript,Android,Opengl Es 2.0,Renderscript,我有一个Renderscript,它处理在分配的输出中给出的图像。 我想在OpenGL程序中将此分配用作纹理,但我不知道如何从分配中获取纹理ID 另一方面,我知道我可以使用graphic Renderscript,但由于它已被弃用,我想一定有其他方法可以达到相同的效果。在创建分配时指定使用情况输出。假设您正在脚本中生成纹理数据,您还将添加使用情况\脚本。然后你可以打电话 分配.setSurface(theGLSurface) 将分配链接到纹理。每次要使用需要调用的脚本内容更新纹理时 Alloca

我有一个Renderscript,它处理在分配的输出中给出的图像。 我想在OpenGL程序中将此分配用作纹理,但我不知道如何从分配中获取纹理ID


另一方面,我知道我可以使用graphic Renderscript,但由于它已被弃用,我想一定有其他方法可以达到相同的效果。

在创建分配时指定使用情况输出。假设您正在脚本中生成纹理数据,您还将添加使用情况\脚本。然后你可以打电话

分配.setSurface(theGLSurface)

将分配链接到纹理。每次要使用需要调用的脚本内容更新纹理时

Allocation.ioSend()


这将在不制作额外副本的情况下移动数据。

您已经用将分配转换为 一个纹理,但更容易考虑创建一个纹理,然后 该纹理的分配所有权。这样,你就会知道 将纹理ID传递给分配之前。注意:分配 具有更改纹理属性的功能

一步一步地:
  • 创建GL纹理

  • 创建一个

  • 使用usage
    Allocation.usage\u IO\u OUTPUT\124; Allocation.usage\u SCRIPT创建分配

  • 使用
    setSurface()

  • 将数据放入分配中

  • 在分配上调用
    ioSend()
    ,以更新纹理

  • 如果要更新纹理,请重复步骤5和6

  • 我离GL专家很远,所以第2步是坦率的猜测。 下面是HelloComputer示例的改编,我在其中替换了
    display out
    带有, 这是一个为我轻松创建纹理和表面纹理的视图。 从那时起,我遵循上述步骤

    package com.example.android.rs.hellocompute;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.SurfaceTexture;
    import android.os.Bundle;
    import android.renderscript.Allocation;
    import android.renderscript.Element;
    import android.renderscript.RenderScript;
    import android.renderscript.Type;
    import android.view.Surface;
    import android.view.Surface.OutOfResourcesException;
    import android.view.TextureView;
    import android.widget.ImageView;
    
    public class HelloCompute extends Activity {
        private Bitmap mBitmapIn;
    
        private RenderScript mRS;
        private ScriptC_mono mScript;
        private Allocation mSurfaceAllocation;
        private Allocation mCanvasAllocation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mBitmapIn = loadBitmap(R.drawable.data);
            int x = mBitmapIn.getWidth();
            int y = mBitmapIn.getHeight();
    
            ImageView in = (ImageView) findViewById(R.id.displayin);
            in.setImageBitmap(mBitmapIn);
    
            mRS = RenderScript.create(this);
            mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);
    
            mCanvasAllocation = Allocation.createTyped(mRS, 
                    new Type.Builder(mRS, Element.RGBA_8888(mRS))
                        .setX(x).setY(y).create(), 
                    Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
    
            mSurfaceAllocation = Allocation.createTyped(mRS, 
                    new Type.Builder(mRS, Element.RGBA_8888(mRS))
                        .setX(x).setY(y).create(), 
                    Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
    
            TextureView mTextureView = (TextureView) findViewById(R.id.displayout);
            mTextureView.setSurfaceTextureListener(
              new TextureView.SurfaceTextureListener() {
                @Override
                public void onSurfaceTextureAvailable(SurfaceTexture s, 
                        int w, int h) {
                    if(s != null) mSurfaceAllocation.setSurface(new Surface(s));
                    mScript.forEach_root(mCanvasAllocation, mSurfaceAllocation);
                    mSurfaceAllocation.ioSend();
                }
    
                @Override
                public void onSurfaceTextureUpdated(SurfaceTexture s) {
                }
    
                @Override
                public void onSurfaceTextureSizeChanged(SurfaceTexture s, 
                        int w, int h) {
                    if(s != null) mSurfaceAllocation.setSurface(new Surface(s));
                }
    
                @Override
                public boolean onSurfaceTextureDestroyed(SurfaceTexture s) {
                    mSurfaceAllocation.setSurface(null);
                    return true;
                }
            });
    
            try {
                Surface surface = mCanvasAllocation.getSurface();
                Canvas canvas = surface.lockCanvas(new Rect(0,0,100,100));
                canvas.drawBitmap(mBitmapIn, 0, 0, new Paint());
                surface.unlockCanvasAndPost(canvas);
                mCanvasAllocation.ioReceive();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (OutOfResourcesException e) {
                e.printStackTrace();
            }
        }
    
        private Bitmap loadBitmap(int resource) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            return BitmapFactory.decodeResource(getResources(), resource, options);
        }
    }
    

    因此,我想我必须执行
    分配.setSurface(新曲面(新SurfaceTexture(textureID))
    以将输出的分配绑定到纹理ID。我正在运行脚本,然后将纹理渲染到屏幕上,但我看不到渲染中的任何更改,由于脚本从未运行过,我尝试了此操作,但在释放Renderscript上下文时会导致崩溃,请参阅