Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Renderscript scripts on Android Studio, and make them run?

Background

I want to research about creating Renderscript scripts on Android and Renderscript in general, and over the past year, Android-Studio became the only IDE that Google supports for Android apps development.

The problem

For this, I've found multiple websites, as such:

  • https://developer.android.com/guide/topics/renderscript/compute.html#writing-an-rs-kernel
  • How to use the Renderscript Support Library with Gradle
  • http://developer.android.com/guide/topics/renderscript/compute.html
  • https://futurestud.io/blog/how-to-use-the-renderscript-support-library-with-gradle-based-android-projects/
  • http://developer.android.com/guide/topics/renderscript/advanced.html

Thing is, all the tutorials and samples I've seen are for Eclipse, and they say that all I need to do is create an "rs" file inside the "raw" folder (also tried in the "src" folder, in the same folder of the "MainActivity.java" file), and it will auto-generate the needed Java files for me, having a prefix of "ScriptC_".

But it doesn't work for me.

What I've tried

I've created a file from some sample I've found (for Eclipse) called "julia.rs". Here's the code:

#pragma version(1)
#pragma rs java_package_name(lb.com.myapplication)

float cx;
float cy;
float width;
float height;
float zoom;
int precision;

uchar *color;

void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    float fx = (x + 0.5f) / width * (4.f / zoom) - (2.f / zoom);
    float fy = (y + 0.5f) / height * (4.f / zoom) - (2.f / zoom);

    float t = 0;
    int k = 0;

    while(k < precision - 1) {
        t = fx * fx - fy * fy + cx;
        fy = 2 * fx * fy + cy;
        fx = t;
        if (fx * fx + fy * fy >= 4) {
           break;
        }
        k++;
    }
    out->b = color[k*3+0];
    out->g = color[k*3+1];
    out->r = color[k*3+2];
}

In the java file, I wanted to access the newly created file, so I started to write "ScriptC", and expected it to fill the needed extra characters, but it doesn't.

I can't for example use this piece of code:

mScript=new ScriptC_julia(mRS,getResources(),R.raw.julia);

I've also tried to add Renderscript support for older Android versions, but this of course didn't help:

defaultConfig {
    renderscriptTargetApi 22
    renderscriptSupportModeEnabled true
    ...

Another thing I've tried is to use New->Folder->RenderScript folder via the context menu of the app, but then I got this error:

Error:Execution failed for task ':app:compileDebugRenderscript'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\AppData\Local\Android\Sdk\build-tools\23.0.0-preview\llvm-rs-cc.exe'' finished with non-zero exit value -1073741515

The question

What's the correct way to create and run a Renderscript script on Android-Studio?


EDIT: Sadly I have the exact same issue again, and this time, setting renderscriptTargetApi to 18 doesn't help. I've also tried another projects with Renderscript, here and here, but both have the same issues:

Error:Execution failed for task ':renderscript:compileArmDebugRenderscript'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\android\Sdk\build-tools\23.0.1\llvm-rs-cc.exe'' finished with non-zero exit value -1073741515

I've now added a bounty to solve this issue once and for all.

like image 660
android developer Avatar asked Aug 07 '15 07:08

android developer


People also ask

What is RenderScript and when should we really use it?

RenderScript framework is basically based on data parallel computation. It distributes your application workload on all the processors available on your device like multi-core CPUs or GPUs.

What is make in Android Studio?

Make Module Compiled are all the source files that have been modified since the last compilation in the selected module as well as in all the modules it depends on recursively. Rebuild Project All the source files in the project are recompiled.


2 Answers

The only modification your project needed to build successfully was changing renderscriptTargetApi value from 22 to 18. Otherwise Renderscript script compilation produces intermediate files that use 64-bit ABI, which the build process fails to link with precompiled intermediates in 22.0.01 build tools that use 32-bit ABI.

UPDATE: as of September 2015 the new version of build tools (23.0.0) does not work with Renderscript support library, so either you have to disable it or revert the tools to 22.0.01.

like image 140
Miloslaw Smyk Avatar answered Oct 19 '22 09:10

Miloslaw Smyk


Building android apps with renderscript inside is quite different from building a normal android app. Good to see that you got this working in the end :) For anyone else wanting to play around with the source it can be found at github: https://github.com/carlemil/JuliaLiveWallpaper and two other rs project that i worked on: https://github.com/carlemil/alw.plasma and https://github.com/carlemil/alw.eld and if you want to see them "in action" i got them on google play to: https://play.google.com/store/apps/developer?id=Erbsman

like image 26
Carl-Emil Kjellstrand Avatar answered Oct 19 '22 09:10

Carl-Emil Kjellstrand