How to pass array values to and from Android RenderScript using Allocations

后端 未结 3 1728
盖世英雄少女心
盖世英雄少女心 2021-01-11 19:39

I\'ve been working with RenderScript recently with the intent of creating an API that a programmer can use with ease, similar to the way that Microsoft Accelerator works.

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 19:51

    I'm not sure if this will be of help to you at this point but since I know how much of a pain it can be to work through RenderScript, here is the help I can offer. In order to use the rsSendToClient function, you need to instruct the RenderScript instance you created where to send messages to. This is accomplished by something such as:

    private void intAdd(int[] A, int[] B) {
         RenderScript rs = RenderScript.create(this);
    
         MySubclassedRSMessageHandler handler = new MySubclassedRSMessageHandler();
         rs.setMessageHandler(handler);
         ScriptC_rsintadd intaddscript = new ScriptC_rsintadd(rs, getResources(), R.raw.rsintadd);
         mScript = intaddscript;
    
         for(int i = 0; i < A.length; i++) {
              setNewValues(mScript, A[i], B[i]);
              intaddscript.invoke_intAdd();
              int C = getResult(mScript);
              notifyUser.append(" " + C);
         }
    }
    

    It will be necessary to subclass RenderScript.RSMessageHandler and override the run() method. See http://developer.android.com/reference/android/renderscript/RenderScript.RSMessageHandler.html if you havn't already. Basically there is no way to get around the asynchronous nature which I find to be a double edged sword.

    As for the inefficiency, I would consider creating a RenderScript instance, leave it running (you can pause it when not needed, will stay in memory but stop the threads, thus not incurring the construction cost each time you call a function). From here you can have your structures and then use invoke_myFunction(some arguments here) from the reflected Java layer.

    Hopefully this helps at least a little bit.

提交回复
热议问题