1b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypage.title=Graphics
2b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyparent.title=Renderscript
3f197b639ecccc570ee235486f75ba15f87070c28Robert Lyparent.link=index.html
4b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
5f197b639ecccc570ee235486f75ba15f87070c28Robert Ly@jd:body
6f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
7f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <div id="qv-wrapper">
8f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <div id="qv">
9f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <h2>In this document</h2>
10f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
11f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <ol>
12f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        <li>
13849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          <a href="#creating-graphics-rs">Creating a Graphics Renderscript</a>
14849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          <ol>
15b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            <li><a href="#creating-native">Creating the Renderscript file</a></li>
16849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly            <li><a href="#creating-entry">Creating the Renderscript entry point class</a></li>
17b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            <li><a href="#creating-view">Creating the view class</a></li>
18b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            <li><a href="#creating-activity">Creating the activity class</a></li>
19849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          </ol>
20849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly        </li>
21849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly        <li>
22849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          <a href="#drawing">Drawing</a>
23f197b639ecccc570ee235486f75ba15f87070c28Robert Ly          <ol>
24b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            <li><a href="#drawing-rsg">Simple drawing</a></li>
25849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly            <li><a href="#drawing-mesh">Drawing with a mesh</a></li>
26849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          </ol>
27849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly        </li>
28849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly        <li>
29849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          <a href="#shaders">Shaders</a>
30849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly          <ol>
31849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly            <li><a href="#shader-bindings">Shader bindings</a></li>
32849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly            <li><a href="#shader-sampler">Defining a sampler</a></li>
33f197b639ecccc570ee235486f75ba15f87070c28Robert Ly          </ol>
34f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        </li>
35b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <li>
36b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly          <a href="#fbo">Rendering to a Framebuffer Object</a>
37b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        </li>
38f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      </ol>
39f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
40f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <h2>Related Samples</h2>
41f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
42f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <ol>
43f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        <li><a href="{@docRoot}resources/samples/RenderScript/Balls/index.html">Balls</a></li>
44f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
4519aad293c0dba4ed9a47939c487fecdd5318ef08Scott Main        <li><a href="{@docRoot}resources/samples/RenderScript/Fountain/index.html">Fountain</a></li>
46f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
47b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <li><a href="{@docRoot}resources/samples/RenderScript/FountainFbo/index.html">FountainFbo</a></li>        
48b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
4950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <li><a href="{@docRoot}resources/samples/RenderScript/HelloWorld/index.html">Hello
5050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainWorld</a></li>
51f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
5219aad293c0dba4ed9a47939c487fecdd5318ef08Scott Main        <li><a
5319aad293c0dba4ed9a47939c487fecdd5318ef08Scott Mainhref="{@docRoot}resources/samples/RenderScript/MiscSamples/index.html">Misc Samples</a></li>
54f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      </ol>
55f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </div>
56f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </div>
57f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
58b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>Renderscript provides a number of graphics APIs for rendering, both at the Android
59b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  framework level as well as at the Renderscript runtime level. For instance, the Android framework APIs let you
60f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  create meshes and define shaders to customize the graphical rendering pipeline. The native
61b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  Renderscript graphics APIs let you draw the actual meshes to render your scene. You need to
62b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  be familiar with both APIs to appropriately render graphics on an Android-powered device.</p>
63f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
64b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h2 id="creating-graphics-rs">Creating a Graphics Renderscript</h2>
65f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
66b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>Renderscript applications require various layers of code, so it is useful to create the following
67b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  files to help keep your application organized:</p>
68f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
69b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <dl>
70b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dt>The Renderscript <code>.rs</code> file</dt>
71b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
72b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dd>This file contains the logic to do the graphics rendering.</dd>
73f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
74b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dt>The Renderscript entry point <code>.java</code> class</dt>
75b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
76b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dd>This class allows the view class to interact with the code defined in the <code>.rs</code>
77b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    file. This class contains a Renderscript object (instance of
78f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <code>ScriptC_<em>renderscript_file</em></code>), which allows your Android framework code to
79b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    call the Renderscript code. In general, this class does much of the setup for Renderscript
80b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    such as shader and mesh building and memory allocation and binding. The SDK samples follow the
81b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    convention of naming this file ActivityRS.java,
82b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    where Activity is the name of your main activity class.</dd>
83b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
84b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dt>The view <code>.java</code> class</dt>
85b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
86b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dd>This class extends {@link android.renderscript.RSSurfaceView} or {@link
87b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    android.renderscript.RSTextureView} to provide a surface to render on. A {@link
88b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    android.renderscript.RSSurfaceView} consumes a whole window, but a {@link
89b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    android.renderscript.RSTextureView} allows you to draw Renderscript graphics inside of a
90b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    view and add it to a {@link android.view.ViewGroup} alongside
91b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    other views. In this class, you create a {@link android.renderscript.RenderScriptGL} context object
92b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    with a call to {@link android.renderscript.RSSurfaceView#createRenderScriptGL
93b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    RSSurfaceView.createRenderscriptGL()} or {@link android.renderscript.RSTextureView#createRenderScriptGL
94b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    RSTextureView.createRenderscriptGL()}. The {@link android.renderscript.RenderScriptGL} context object
95b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    contains information about the current rendering state of Renderscript such as the vertex and
96b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    fragment shaders. You pass this context object to the Renderscript entry point class, so that
97b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    class can modify the rendering context if needed and bind the Renderscript code to the context. Once bound,
98b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    the view class can use the Renderscript code to display graphics.
99b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    The view class should also implement callbacks for events inherited from {@link
100f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    android.view.View}, such as {@link android.view.View#onTouchEvent onTouchEvent()} and {@link
101b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    android.view.View#onKeyDown onKeyDown()} if you want to detect these types of user interactions.
102b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    The SDK samples follow the convention of naming this file ActivityView.java,
103b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    where Activity is the name of your main activity class</dd>
104f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
105b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dt>The activity <code>.java</code> class</dt>
106b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
107b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <dd>This class is the main activity class and sets your {@link android.renderscript.RSSurfaceView} as the main content
108b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    view for this activity or uses the {@link android.renderscript.RSTextureView} alongside other views.</dd>
109b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </dl>
110b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>Figure 1 describes how these classes interact with one another in a graphics Renderscript:</p>
111b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  
112b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <img src="{@docRoot}images/rs_graphics.png">
113b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p class="img-caption"><strong>Figure 1.</strong> Graphics Renderscript overview</p>
114f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
115f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
116b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The following sections describe how to create an application that uses a graphics Renderscript by using
117b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  the <a href="{@docRoot}resources/samples/RenderScript/Fountain/index.html">Renderscript Fountain
118b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  sample</a> that is provided in the SDK as a guide (some code has been modified from its original
119b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  form for simplicity).</p>
120f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
121b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="creating-native">Creating the Renderscript file</h3>
122b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
123b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>Your Renderscript code resides in <code>.rs</code> and <code>.rsh</code> (headers) files in the
124b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <code>&lt;project_root&gt;/src/</code> directory. This code contains the logic to render your
125b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  graphics and declares all other necessary items such as variables, structs,
126f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  and pointers. Every graphics <code>.rs</code> file generally contains the following items:</p>
127f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
128f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <ul>
129b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>A pragma declaration (<code>#pragma rs java_package_name(<em>package.name</em>)</code>) that declares
130b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    the package name of the <code>.java</code> reflection of this Renderscript.</li>
131f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
132b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>A pragma declaration (<code>#pragma version(1)</code>) that declares the version of Renderscript that
133f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    you are using (1 is the only value for now).</li>
134f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
135b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>A <code>#include "rs_graphics.rsh"</code> declaration.</li>
136f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
137b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>A <code>root()</code> function. This is the main worker function for your Renderscript and
138b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    calls Renderscript graphics functions to render scenes. This function is called every time a
139b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    frame refresh occurs, which is specified as its return value. A <code>0</code> (zero) specified for
140b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    the return value says to only render the frame when a property of the scene that you are
141f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    rendering changes. A non-zero positive integer specifies the refresh rate of the frame in
142f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    milliseconds.
143f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
144b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      <p class="note"><strong>Note:</strong> The Renderscript runtime makes its best effort to
145f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      refresh the frame at the specified rate. For example, if you are creating a live wallpaper
146f11ffc1178645762b74f945b6c5ee068dc6000d3Robert Ly      and set the return value to 20, the Renderscript runtime renders the wallpaper at 50fps if it has just
147b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      enough or more resources to do so. It renders as fast as it can if not enough resources
148b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      are available.</p>
149b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
150b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      <p>For more information on using the Renderscript graphics functions, see the <a href=
151849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly      "#drawing">Drawing</a> section.</p>
152f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </li>
153f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
154b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>An <code>init()</code> function. This allows you to do initialization of your
155b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Renderscript before the <code>root()</code> function runs, such as assigning values to variables. This
156b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    function runs once and is called automatically when the Renderscript starts, before anything
157b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    else in your Renderscript. Creating this function is optional.</li>
158f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
159b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>Any variables, pointers, and structures that you wish to use in your Renderscript code (can
160f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    be declared in <code>.rsh</code> files if desired)</li>
161f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </ul>
162f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
163b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The following code shows how the <code>fountain.rs</code> file is implemented:</p>
164f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
165f197b639ecccc570ee235486f75ba15f87070c28Robert Ly#pragma version(1)
166f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
167f197b639ecccc570ee235486f75ba15f87070c28Robert Ly// Tell which java package name the reflected files should belong to
168b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly#pragma rs java_package_name(com.example.android.rs.fountain)
169f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
170b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly//declare shader binding
171b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly#pragma stateFragment(parent)
172b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
173b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly// header with graphics APIs, must include explicitly
174f197b639ecccc570ee235486f75ba15f87070c28Robert Ly#include "rs_graphics.rsh"
175f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
176b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lystatic int newPart = 0;
177b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
178b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly// the mesh to render
179b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyrs_mesh partMesh;
180b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
181b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly// the point representing where a particle is rendered
182b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lytypedef struct __attribute__((packed, aligned(4))) Point {
183b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float2 delta;
184b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float2 position;
185b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    uchar4 color;
186b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly} Point_t;
187b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LyPoint_t *point;
188b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
189b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly// main worker function that renders particles onto the screen
190b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyint root() {
191b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float dt = min(rsGetDt(), 0.1f);
192b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgClearColor(0.f, 0.f, 0.f, 1.f);
193b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    const float height = rsgGetHeight();
194b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    const int size = rsAllocationGetDimX(rsGetAllocation(point));
195b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float dy2 = dt * (10.f);
196b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Point_t * p = point;
197b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    for (int ct=0; ct &lt; size; ct++) {
198b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p-&gt;delta.y += dy2;
199b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p-&gt;position += p-&gt;delta;
200b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if ((p-&gt;position.y &gt; height) &amp;&amp; (p-&gt;delta.y &gt; 0)) {
201b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            p-&gt;delta.y *= -0.3f;
202b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
203b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p++;
204b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    }
205b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
206b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgDrawMesh(partMesh);
207b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    return 1;
208b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly}
209f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
210b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly// adds particles to the screen to render
211b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lystatic float4 partColor[10];
212b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyvoid addParticles(int rate, float x, float y, int index, bool newColor)
213b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly{
214b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    if (newColor) {
215b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        partColor[index].x = rsRand(0.5f, 1.0f);
216b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        partColor[index].y = rsRand(1.0f);
217b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        partColor[index].z = rsRand(1.0f);
218b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    }
219b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float rMax = ((float)rate) * 0.02f;
220b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    int size = rsAllocationGetDimX(rsGetAllocation(point));
221b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    uchar4 c = rsPackColorTo8888(partColor[index]);
222b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
223b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Point_t * np = &amp;point[newPart];
224b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float2 p = {x, y};
225b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    while (rate--) {
226b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        float angle = rsRand(3.14f * 2.f);
227b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        float len = rsRand(rMax);
228b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        np-&gt;delta.x = len * sin(angle);
229b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        np-&gt;delta.y = len * cos(angle);
230b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        np-&gt;position = p;
231b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        np-&gt;color = c;
232b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        newPart++;
233b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        np++;
234b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (newPart &gt;= size) {
235b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            newPart = 0;
236b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            np = &amp;point[newPart];
237b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
238b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    }
239f197b639ecccc570ee235486f75ba15f87070c28Robert Ly}
240b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</pre>
241f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
242b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="creating-entry">Creating the Renderscript entry point class</h3>
243f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
244b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>When you create a Renderscript (<code>.rs</code>) file, it is helpful to create a
245b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  corresponding Android framework class that is an entry point into the <code>.rs</code> file.
246b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  The most important thing this class does is receive a {@link android.renderscript.RenderScriptGL} rendering context
247b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  object from the <a href="#creating-view">view class</a> and binds the actual Renderscript
248b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  code to the rendering context. This notifies your view class of the code that it needs
249b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  to render graphics.
250b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </p>
251f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
252b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>In addition, this class should contain all of the things needed to set up Renderscript.
253b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  Some important things that you need to do in this class are:</p>
254f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
255b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <ul>
256b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>Create a Renderscript object 
257b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <code>ScriptC_<em>rs_filename</em></code>. The Renderscript object is attached to the Renderscript bytecode, which is platform-independent and
258b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  gets compiled on the device when the Renderscript application runs. The bytecode is referenced
259b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  as a raw resource and is passed into the constructor for the Renderscript object.
260b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  For example, this is how the <a href="{@docRoot}resources/samples/RenderScript/Fountain/index.html">Fountain</a>
261b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  sample creates the Renderscript object:
262b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <pre>
263b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  RenderScriptGL rs;  //obtained from the view class
264b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  Resources res;      //obtained from the view class
265b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  ...
266b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  ScriptC_fountain mScript = new ScriptC_fountain(mRS, mRes, R.raw.fountain);
267b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </pre>
268b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </li>
269b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Allocate any necessary memory and bind it to your Renderscript code via the Renderscript object.</li>
270b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Build any necessary meshes and bind them to the Renderscript code via the Renderscript object.</li>
271b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Create any necessary programs and bind them to the Renderscript code via the Renderscript object.</li>
272b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </ul>
273b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
274b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The following code shows how the <a href=
275b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  "{@docRoot}resources/samples/RenderScript/Fountain/src/com/example/android/rs/fountain/FountainRS.html">
276b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  FountainRS</a> class is implemented:</p>
277f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
278b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypackage com.example.android.rs.fountain;
279f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
280f197b639ecccc570ee235486f75ba15f87070c28Robert Lyimport android.content.res.Resources;
281f197b639ecccc570ee235486f75ba15f87070c28Robert Lyimport android.renderscript.*;
282b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyimport android.util.Log;
283f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
284b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypublic class FountainRS {
285b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    public static final int PART_COUNT = 50000;
286f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
287b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    public FountainRS() {
288f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
289f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
290f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    /**
291b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * This provides us with the Renderscript context and resources
292b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * that allow us to create the Renderscript object
293f197b639ecccc570ee235486f75ba15f87070c28Robert Ly     */
294b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private Resources mRes;
295b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private RenderScriptGL mRS;
296b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
297b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    // Renderscript object
298b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private ScriptC_fountain mScript;
299b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
300b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    // Called by the view class to initialize the Renderscript context and renderer
301f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    public void init(RenderScriptGL rs, Resources res) {
302f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        mRS = rs;
303f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        mRes = res;
304b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
305b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        /**
306b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly         * Create a shader and bind to the Renderscript context
307b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly         */
308b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
309b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        pfb.setVaryingColor(true);
310b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        rs.bindProgramFragment(pfb.create());
311b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
312b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        /**
313b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly         * Allocate memory for the particles to render and create the mesh to draw
314b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly         */
315b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT);
316b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
317b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        smb.addVertexAllocation(points.getAllocation());
318b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        smb.addIndexSetType(Mesh.Primitive.POINT);
319b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Mesh sm = smb.create();
320b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
321b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly       /**
322b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        * Create and bind the Renderscript object to the Renderscript context
323b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        */
324b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mScript = new ScriptC_fountain(mRS, mRes, R.raw.fountain);
325b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mScript.set_partMesh(sm);
326b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mScript.bind_point(points);
327b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mRS.bindRootScript(mScript);
328f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
329b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
330b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    boolean holdingColor[] = new boolean[10];
331b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
332f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    /**
333b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * Calls Renderscript functions (invoke_addParticles)
334b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * via the Renderscript object to add particles to render
335b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * based on where a user touches the screen.
336f197b639ecccc570ee235486f75ba15f87070c28Robert Ly     */
337b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    public void newTouchPosition(float x, float y, float pressure, int id) {
338b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (id &gt;= holdingColor.length) {
339b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            return;
340b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
341b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        int rate = (int)(pressure * pressure * 500.f);
342b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (rate &gt; 500) {
343b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            rate = 500;
344b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
345b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (rate &gt; 0) {
346b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            mScript.invoke_addParticles(rate, x, y, id, !holdingColor[id]);
347b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            holdingColor[id] = true;
348b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        } else {
349b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            holdingColor[id] = false;
350b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
351b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
352f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
353f197b639ecccc570ee235486f75ba15f87070c28Robert Ly}
354f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
355f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
356f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
357b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="creating-view">Creating the view class</h3>
358b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
359b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
360b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>To display graphics, you need a view to render on. Create a class that extends {@link
361b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  android.renderscript.RSSurfaceView} or {@link android.renderscript.RSTextureView}. This class
362b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  allows you to create a {@link android.renderscript.RenderScriptGL} context object by calling and
363b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  pass it to the Rendscript entry point class to bind the two. Once bound, the content is aware
364b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  of the code that it needs to use to render graphics with. If your Renderscript code
365b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  depends on any type of information that the view is aware of, such as touches from the user,
366b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  you can also use this class to relay that information to the Renderscript entry point class.
367b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  The following code shows how the <code>FountainView</code> class is implemented:</p>
368f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
369b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypackage com.example.android.rs.fountain;
370f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
371b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyimport android.renderscript.RSTextureView;
372f197b639ecccc570ee235486f75ba15f87070c28Robert Lyimport android.renderscript.RenderScriptGL;
373f197b639ecccc570ee235486f75ba15f87070c28Robert Lyimport android.content.Context;
374f197b639ecccc570ee235486f75ba15f87070c28Robert Lyimport android.view.MotionEvent;
375f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
376b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypublic class FountainView extends RSTextureView {
377f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
378b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    public FountainView(Context context) {
379f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        super(context);
380f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
381b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    // Renderscript context
382b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private RenderScriptGL mRS;
383b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    // Renderscript entry point object that calls Renderscript code
384b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private FountainRS mRender;
385f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
386b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    /**
387b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * Create Renderscript context and initialize Renderscript entry point
388b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     */
389b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
390b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    protected void onAttachedToWindow() {
391b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        super.onAttachedToWindow();
392b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        android.util.Log.e("rs", "onAttachedToWindow");
393f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        if (mRS == null) {
394f197b639ecccc570ee235486f75ba15f87070c28Robert Ly            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
395f197b639ecccc570ee235486f75ba15f87070c28Robert Ly            mRS = createRenderScriptGL(sc);
396b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            mRender = new FountainRS();
397f197b639ecccc570ee235486f75ba15f87070c28Robert Ly            mRender.init(mRS, getResources());
398f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        }
399f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
400f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
401b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
402f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    protected void onDetachedFromWindow() {
403b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        super.onDetachedFromWindow();
404b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        android.util.Log.e("rs", "onDetachedFromWindow");
405f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        if (mRS != null) {
406f197b639ecccc570ee235486f75ba15f87070c28Robert Ly            mRS = null;
407f197b639ecccc570ee235486f75ba15f87070c28Robert Ly            destroyRenderScriptGL();
408f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        }
409f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
410f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
411b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
412f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    /**
413b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly     * Use callbacks to relay data to Renderscript entry point class
414f197b639ecccc570ee235486f75ba15f87070c28Robert Ly     */
415b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
416b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    public boolean onTouchEvent(MotionEvent ev)
417b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    {
418b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        int act = ev.getActionMasked();
419b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (act == ev.ACTION_UP) {
420b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0));
421b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            return false;
422b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        } else if (act == MotionEvent.ACTION_POINTER_UP) {
423b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            // only one pointer going up, we can get the index like this
424b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            int pointerIndex = ev.getActionIndex();
425b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            int pointerId = ev.getPointerId(pointerIndex);
426b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            mRender.newTouchPosition(0, 0, 0, pointerId);
427f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        }
428b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        int count = ev.getHistorySize();
429b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        int pcount = ev.getPointerCount();
430b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
431b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        for (int p=0; p &lt; pcount; p++) {
432b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            int id = ev.getPointerId(p);
433b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            mRender.newTouchPosition(ev.getX(p),
434b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                     ev.getY(p),
435b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                     ev.getPressure(p),
436b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                     id);
437b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
438b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            for (int i=0; i &lt; count; i++) {
439b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                mRender.newTouchPosition(ev.getHistoricalX(p, i),
440b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                         ev.getHistoricalY(p, i),
441b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                         ev.getHistoricalPressure(p, i),
442b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                                         id);
443b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            }
444b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
445b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        return true;
446f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
447f197b639ecccc570ee235486f75ba15f87070c28Robert Ly}
448f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
449f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
450b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="creating-activity">Creating the activity class</h3>
451f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
452b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>Applications that use Renderscript still behave like normal Android applications, so you
453b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly   need an activity class that handles activity lifecycle callback events appropriately. The activity class
454b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly   also sets your {@link android.renderscript.RSSurfaceView} view class to be the main content view of the
455b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly   activity or uses your {@link android.renderscript.RSTextureView}
456b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  in a {@link android.view.ViewGroup} alongside other views.</p>
457b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
458b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The following code shows how the <a href="{@docRoot}resources/samples/RenderScript/Fountain/index.html">Fountain</a>
459b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  sample declares its activity class:</p>
460f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
461b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypackage com.example.android.rs.fountain;
462b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
463b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyimport android.app.Activity;
464b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyimport android.os.Bundle;
465b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyimport android.util.Log;
466b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
467b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypublic class Fountain extends Activity {
468b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
469b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private static final String LOG_TAG = "libRS_jni";
470b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private static final boolean DEBUG  = false;
471b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private static final boolean LOG_ENABLED = false;
472f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
473b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    private FountainView mView;
474f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
475b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
476f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    public void onCreate(Bundle icicle) {
477f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        super.onCreate(icicle);
478b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
479b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // Create our Preview view and set it as 
480b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // the content of our activity
481b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mView = new FountainView(this);
482b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        setContentView(mView);
483f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
484f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
485b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
486f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    protected void onResume() {
487b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Log.e("rs", "onResume");
488b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
489b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // Ideally a game should implement onResume() and onPause()
490b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // to take appropriate action when the activity looses focus
491f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        super.onResume();
492b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mView.resume();
493f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
494f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
495b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    &#064;Override
496f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    protected void onPause() {
497b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Log.e("rs", "onPause");
498b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
499b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // Ideally a game should implement onResume() and onPause()
500b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        // to take appropriate action when the activity looses focus
501f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        super.onPause();
502b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        mView.pause();
503b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
504b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    }
505b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
506b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    static void log(String message) {
507b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if (LOG_ENABLED) {
508b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            Log.v(LOG_TAG, message);
509b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
510f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    }
511f197b639ecccc570ee235486f75ba15f87070c28Robert Ly}
512f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
513f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
514b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<p>Now that you have an idea of what is involved in a Renderscript graphics application, you can
515b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lystart building your own. It might be easiest to begin with one of the
516b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<a href="{@docRoot}resources/samples/RenderScript/index.html">Renderscript samples</a> as a starting
517b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypoint if this is your first time using Renderscript.</p>
518b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
519849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly  <h2 id="drawing">Drawing</h2>
520849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly  <p>The following sections describe how to use the graphics functions to draw with Renderscript.</p>
521f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
522b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="drawing-rsg">Simple drawing</h3>
523b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
524b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The native Renderscript APIs provide a few convenient functions to easily draw a polygon or text to
525b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  the screen. You call these in your <code>root()</code> function to have them render to the {@link
526b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  android.renderscript.RSSurfaceView} or {@link android.renderscript.RSTextureView}. These functions are
527b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  available for simple drawing and should not be used for complex graphics rendering:</p>
528f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
529f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <ul>
530f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <li><code>rsgDrawRect()</code>: Sets up a mesh and draws a rectangle to the screen. It uses the
531f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    top left vertex and bottom right vertex of the rectangle to draw.</li>
532f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
533f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <li><code>rsgDrawQuad()</code>: Sets up a mesh and draws a quadrilateral to the screen.</li>
534f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
535b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>rsgDrawQuadTexCoords()</code>: Sets up a mesh and draws a quadrilateral to the screen
536b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    using the provided coordinates of a texture.</li>
537b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
538b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>rsgDrawText()</code>: Draws specified text to the screen. Use <code>rsgFontColor()</code>
539b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    to set the color of the text.</li>
540f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </ul>
541f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
542849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly  <h3 id="drawing-mesh">Drawing with a mesh</h3>
543f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
544b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>When you want to render complex scenes to the screen, instantiate a {@link
545b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  android.renderscript.Mesh} and draw it with <code>rsgDrawMesh()</code>. A {@link
546f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  android.renderscript.Mesh} is a collection of allocations that represent vertex data (positions,
547b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  normals, texture coordinates) and index data that provides information on how to draw triangles
548b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  and lines with the provided vertex data. You can build a Mesh in three different ways:</p>
549f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
550f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <ul>
551f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <li>Build the mesh with the {@link android.renderscript.Mesh.TriangleMeshBuilder} class, which
552b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    allows you to specify a set of vertices and indices for each triangle that you want to draw.</li>
553f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
554f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <li>Build the mesh using an {@link android.renderscript.Allocation} or a set of {@link
555f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    android.renderscript.Allocation}s with the {@link android.renderscript.Mesh.AllocationBuilder}
556b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    class. This approach allows you to build a mesh with vertices already stored in memory, which allows you
557b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    to specify the vertices in Renderscript or Android framework code.</li>
558f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
559b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li>Build the mesh with the {@link android.renderscript.Mesh.Builder} class. You should use
560b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    this convenience method when you know the data types you want to use to build your mesh, but
561f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    don't want to make separate memory allocations like with {@link
562f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    android.renderscript.Mesh.AllocationBuilder}. You can specify the types that you want and this
563f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    mesh builder automatically creates the memory allocations for you.</li>
564f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </ul>
565f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
566f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>To create a mesh using the {@link android.renderscript.Mesh.TriangleMeshBuilder}, you need to
567f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  supply it with a set of vertices and the indices for the vertices that comprise the triangle. For
568f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  example, the following code specifies three vertices, which are added to an internal array,
569f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  indexed in the order they were added. The call to {@link
570f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  android.renderscript.Mesh.TriangleMeshBuilder#addTriangle addTriangle()} draws the triangle with
571f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  vertex 0, 1, and 2 (the vertices are drawn counter-clockwise).</p>
572f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
573f197b639ecccc570ee235486f75ba15f87070c28Robert Lyint float2VtxSize = 2;
574f11ffc1178645762b74f945b6c5ee068dc6000d3Robert LyMesh.TriangleMeshBuilder triangles = new Mesh.TriangleMeshBuilder(renderscriptGL,
575f197b639ecccc570ee235486f75ba15f87070c28Robert Lyfloat2VtxSize, Mesh.TriangleMeshBuilder.COLOR);
576f197b639ecccc570ee235486f75ba15f87070c28Robert Lytriangles.addVertex(300.f, 300.f);
577f197b639ecccc570ee235486f75ba15f87070c28Robert Lytriangles.addVertex(150.f, 450.f);
578f197b639ecccc570ee235486f75ba15f87070c28Robert Lytriangles.addVertex(450.f, 450.f);
579f197b639ecccc570ee235486f75ba15f87070c28Robert Lytriangles.addTriangle(0 , 1, 2);
580f197b639ecccc570ee235486f75ba15f87070c28Robert LyMesh smP = triangle.create(true);
581f197b639ecccc570ee235486f75ba15f87070c28Robert Lyscript.set_mesh(smP);
582f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
583f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
584f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>To draw a mesh using the {@link android.renderscript.Mesh.AllocationBuilder}, you need to
585f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  supply it with one or more allocations that contain the vertex data:</p>
586f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
587f197b639ecccc570ee235486f75ba15f87070c28Robert LyAllocation vertices;
588f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
589f197b639ecccc570ee235486f75ba15f87070c28Robert Ly...
590f197b639ecccc570ee235486f75ba15f87070c28Robert LyMesh.AllocationBuilder triangle = new Mesh.AllocationBuilder(mRS);
591f197b639ecccc570ee235486f75ba15f87070c28Robert Lysmb.addVertexAllocation(vertices.getAllocation());
592f197b639ecccc570ee235486f75ba15f87070c28Robert Lysmb.addIndexSetType(Mesh.Primitive.TRIANGLE);
593f197b639ecccc570ee235486f75ba15f87070c28Robert LyMesh smP = smb.create();
594f197b639ecccc570ee235486f75ba15f87070c28Robert Lyscript.set_mesh(smP);
595f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
596f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
597b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>In your Renderscript code, draw the built mesh to the screen:</p>
598f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
599f197b639ecccc570ee235486f75ba15f87070c28Robert Lyrs_mesh mesh;
600f197b639ecccc570ee235486f75ba15f87070c28Robert Ly...
601f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
602f197b639ecccc570ee235486f75ba15f87070c28Robert Lyint root(){
603f197b639ecccc570ee235486f75ba15f87070c28Robert Ly...
604f197b639ecccc570ee235486f75ba15f87070c28Robert LyrsgDrawMesh(mesh);
605f197b639ecccc570ee235486f75ba15f87070c28Robert Ly...
606f197b639ecccc570ee235486f75ba15f87070c28Robert Lyreturn 0; //specify a non zero, positive integer to specify the frame refresh.
607f197b639ecccc570ee235486f75ba15f87070c28Robert Ly          //0 refreshes the frame only when the mesh changes.
608f197b639ecccc570ee235486f75ba15f87070c28Robert Ly}
609f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
610f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
611b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h2 id="shader">Programs</h2>
612f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
613f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>You can attach four program objects to the {@link android.renderscript.RenderScriptGL} context
614f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  to customize the rendering pipeline. For example, you can create vertex and fragment shaders in
615b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  GLSL or build a raster program object that controls culling. The four programs mirror a
616b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  traditional graphical rendering pipeline:</p>
617f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
618f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <table>
619f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <tr>
620f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <th>Android Object Type</th>
621f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
622b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      <th>Renderscript Native Type</th>
623f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
624f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <th>Description</th>
625f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </tr>
626f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
627f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <tr>
628f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>{@link android.renderscript.ProgramVertex}</td>
629f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
630f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>rs_program_vertex</td>
631f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
632f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>
633b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>The Renderscript vertex program, also known as a vertex shader, describes the stage in
634f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        the graphics pipeline responsible for manipulating geometric data in a user-defined way.
635b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        The object is constructed by providing Renderscript with the following data:</p>
636f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
637f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        <ul>
638b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly          <li>An {@link android.renderscript.Element} describing its varying inputs or attributes</li>
639f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
640f197b639ecccc570ee235486f75ba15f87070c28Robert Ly          <li>GLSL shader string that defines the body of the program</li>
641f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
642b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly          <li>a {@link android.renderscript.Type} that describes the layout of an
643b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly          Allocation containing constant or uniform inputs</li>
644f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        </ul>
645f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
646f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        <p>Once the program is created, bind it to the {@link android.renderscript.RenderScriptGL}
647f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        graphics context by calling {@link android.renderscript.RenderScriptGL#bindProgramVertex
648f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        bindProgramVertex()}. It is then used for all subsequent draw calls until you bind a new
649f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        program. If the program has constant inputs, the user needs to bind an allocation
650f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        containing those inputs. The allocation's type must match the one provided during creation.
651b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        </p>
652b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
653b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>The Renderscript runtime then does all the necessary plumbing to send those constants to
654f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        the graphics hardware. Varying inputs to the shader, such as position, normal, and texture
655b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        coordinates are matched by name between the input {@link android.renderscript.Element}
656b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        and the mesh object that is being drawn. The signatures don't have to be exact or in any
657b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        strict order. As long as the input name in the shader matches a channel name and size
658b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        available on the mesh, the Renderscript runtime handles connecting the two. Unlike OpenGL
659b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        there is no need to link the vertex and fragment programs.</p>
660b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
661b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>To bind shader constants to the program, declare a <code>struct</code> that contains the necessary
662b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        shader constants in your Renderscript code. This <code>struct</code> is generated into a
663b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        reflected class that you can use as a constant input element during the program's creation.
664b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        It is an easy way to create an instance of this <code>struct</code> as an allocation. You would then
665b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        bind this {@link android.renderscript.Allocation} to the program and the
666b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Renderscript runtime sends the data that is contained in the <code>struct</code> to the hardware
667b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        when necessary. To update shader constants, you change the values in the
668b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        {@link android.renderscript.Allocation} and notify the Renderscript
669b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        code of the change.</p>
670b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
671b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>The {@link android.renderscript.ProgramVertexFixedFunction.Builder} class also
672b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        lets you build a simple vertex shader without writing GLSL code.
673b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        </p>
674f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      </td>
675f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </tr>
676f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
677f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <tr>
678f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>{@link android.renderscript.ProgramFragment}</td>
679f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
680f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>rs_program_fragment</td>
681f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
682f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>
683b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>The Renderscript fragment program, also known as a fragment shader, is responsible for
684f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        manipulating pixel data in a user-defined way. It's constructed from a GLSL shader string
685b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        containing the program body, texture inputs, and a {@link android.renderscript.Type}
686b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        object that describes the constants
687b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        used by the program. Like the vertex programs, when an {@link android.renderscript.Allocation}
688b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        with constant input
689f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        values is bound to the shader, its values are sent to the graphics program automatically.
690b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Note that the values inside the {@link android.renderscript.Allocation} are not explicitly tracked.
691b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        If they change between two draw calls using the same program object, notify the runtime of that change by
692b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        calling <code>rsgAllocationSyncAll()</code>, so it can send the new values to hardware. Communication
693f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        between the vertex and fragment programs is handled internally in the GLSL code. For
694b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        example, if the fragment program is expecting a varying input called <code>varTex0</code>, the GLSL code
695f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        inside the program vertex must provide it.</p>
696f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
697b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>To bind shader constructs to the program, declare a <code>struct</code> that contains the necessary
698b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        shader constants in your Renderscript code. This <code>struct</code> is generated into a
699b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        reflected class that you can use as a constant input element during the program's creation.
700b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        It is an easy way to create an instance of this <code>struct</code> as an allocation. You would then
701b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        bind this {@link android.renderscript.Allocation} to the program and the
702b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        Renderscript runtime sends the data that is contained in the <code>struct</code> to the hardware
703b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        when necessary. To update shader constants, you change the values in the
704b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        {@link android.renderscript.Allocation} and notify the Renderscript
705b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        code of the change.</p>
706b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
707b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        <p>The {@link android.renderscript.ProgramFragmentFixedFunction.Builder} class also
708b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        lets you build a simple fragment shader without writing GLSL code.
709b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        </p>
710f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      </td>
711f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </tr>
712f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
713f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <tr>
714f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>{@link android.renderscript.ProgramStore}</td>
715f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
716f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>rs_program_store</td>
717f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
718b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      <td>The Renderscript store program contains a set of parameters that control how the graphics
719f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      hardware writes to the framebuffer. It could be used to enable and disable depth writes and
720f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      testing, setup various blending modes for effects like transparency and define write masks
721f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      for color components.</td>
722f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </tr>
723f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
724f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    <tr>
725f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>{@link android.renderscript.ProgramRaster}</td>
726f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
727f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      <td>rs_program_raster</td>
728f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
729b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly      <td>The Renderscript raster program is primarily used to specify whether point sprites are enabled and to
730f197b639ecccc570ee235486f75ba15f87070c28Robert Ly      control the culling mode. By default back faces are culled.</td>
731f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    </tr>
732f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </table>
733f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
734b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>The following example defines a vertex shader in GLSL and binds it to a Renderscript context object:</p>
735f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
736f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    private RenderScriptGL glRenderer;      //rendering context
737f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    private ScriptField_Point mPoints;      //vertices
738f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    private ScriptField_VpConsts mVpConsts; //shader constants
739f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
740f197b639ecccc570ee235486f75ba15f87070c28Robert Ly    ...
741f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
742f197b639ecccc570ee235486f75ba15f87070c28Robert Ly     ProgramVertex.Builder sb = new ProgramVertex.Builder(glRenderer);
743f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        String t =  "varying vec4 varColor;\n" +
744f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "void main() {\n" +
745f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "  vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n" +
746f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "  pos.xy = ATTRIB_position;\n" +
747f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "  gl_Position = UNI_MVP * pos;\n" +
748f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "  varColor = vec4(1.0, 1.0, 1.0, 1.0);\n" +
749f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "  gl_PointSize = ATTRIB_size;\n" +
750f197b639ecccc570ee235486f75ba15f87070c28Robert Ly                    "}\n";
751f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        sb.setShader(t);
752f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        sb.addConstant(mVpConsts.getType());
753f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        sb.addInput(mPoints.getElement());
754f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        ProgramVertex pvs = sb.create();
755f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        pvs.bindConstants(mVpConsts.getAllocation(), 0);
756f197b639ecccc570ee235486f75ba15f87070c28Robert Ly        glRenderer.bindProgramVertex(pvs);
757f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
758f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
759b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
760f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>The <a href=
761f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  "{@docRoot}resources/samples/RenderScript/MiscSamples/src/com/example/android/rs/miscsamples/RsRenderStatesRS.html">
762f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  RsRenderStatesRS</a> sample has many examples on how to create a shader without writing GLSL.</p>
763f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
764b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <h3 id="shader-bindings">Program bindings</h3>
765f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
766b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>You can also declare four pragmas that control default program bindings to the {@link
767f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  android.renderscript.RenderScriptGL} context when the script is executing:</p>
768f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
769f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <ul>
770b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>stateVertex</code></li>
771f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
772b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>stateFragment</code></li>
773f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
774b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>stateRaster</code></li>
775f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
776b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    <li><code>stateStore</code></li>
777f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  </ul>
778f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
779f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>The possible values for each pragma are <code>parent</code> or <code>default</code>. Using
780b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <code>default</code> binds the shaders to the graphical context with the system defaults.</p>
781f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
782f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>Using <code>parent</code> binds the shaders in the same manner as it is bound in the calling
783f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  script. If this is the root script, the parent state is taken from the bind points that are set
784f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  by the {@link android.renderscript.RenderScriptGL} bind methods.</p>
785f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
786b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <p>For example, you can define this at the top of your graphics Renderscript code to have
787b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  the vertex and store programs inherent the bind properties from their parent scripts:</p>
788f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <pre>
789f197b639ecccc570ee235486f75ba15f87070c28Robert Ly#pragma stateVertex(parent)
790f197b639ecccc570ee235486f75ba15f87070c28Robert Ly#pragma stateStore(parent)
791f197b639ecccc570ee235486f75ba15f87070c28Robert Ly</pre>
792f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
793849759380ea10fb9d8f3e5d4b2292e43ef560df9Robert Ly  <h3 id="shader-sampler">Defining a sampler</h3>
794f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
795f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>A {@link android.renderscript.Sampler} object defines how data is extracted from textures.
796b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  Samplers are bound to a {@link android.renderscript.ProgramFragment} alongside the texture
797b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  whose sampling they control. These
798b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  objects are used to specify such things as edge clamping behavior, whether mip-maps are used, and
799b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  the amount of anisotropy required. There might be situations where hardware does not support the
800b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  desired behavior of the sampler. In these cases, the Renderscript runtime attempts to provide the
801b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  closest possible approximation. For example, the user requested 16x anisotropy, but only 8x was
802b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  set because it's the best available on the hardware.</p>
803f197b639ecccc570ee235486f75ba15f87070c28Robert Ly
804f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  <p>The <a href=
805f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  "{@docRoot}resources/samples/RenderScript/MiscSamples/src/com/example/android/rs/miscsamples/RsRenderStatesRS.html">
806f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  RsRenderStatesRS</a> sample has many examples on how to create a sampler and bind it to a
807f197b639ecccc570ee235486f75ba15f87070c28Robert Ly  Fragment program.</p>
808b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
809b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
810b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
811b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<h2 id="fbo">Rendering to a Framebuffer Object</h2>
812b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
813b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<p>Framebuffer objects allow you to render offscreen instead of in the default onscreen
814b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyframebuffer. This approach might be useful for situations where you need to post-process a texture before
815b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyrendering it to the screen, or when you want to composite two scenes in one such as rendering a rear-view
816b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lymirror of a car. There are two buffers associated with a framebuffer object: a color buffer
817b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyand a depth buffer. The color buffer (required) contains the actual pixel data of the scene
818b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lythat you are rendering, and the depth buffer (optional) contains the values necessary to figure
819b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyout what vertices are drawn depending on their z-values.</p>
820b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
821b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<p>In general, you need to do the following to render to a framebuffer object:</p>
822b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
823b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<ul>
824b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Create {@link android.renderscript.Allocation} objects for the color buffer and
825b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  depth buffer (if needed). Specify the {@link
826b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  android.renderscript.Allocation#USAGE_GRAPHICS_RENDER_TARGET} usage attribute for these
827b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  allocations to notify the Renderscript runtime to use these allocations for the framebuffer
828b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  object. For the color buffer allocation, you most likely need to declare the {@link
829b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  android.renderscript.Allocation#USAGE_GRAPHICS_TEXTURE} usage attribute
830b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  to use the color buffer as a texture, which is the most common use of the framebuffer object.</li>
831b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
832b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Tell the Renderscript runtime to render to the framebuffer object instead of the default
833b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  framebuffer by calling <code>rsgBindColorTarget()</code> and passing it the color buffer
834b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  allocation. If applicable, call <code>rsgBindDepthTarget()</code> passing in the depth buffer
835b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  allocation as well.</li>
836b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
837b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Render your scene normally with the <code>rsgDraw</code> functions. The scene will be
838b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  rendered into the color buffer instead of the default onscreen framebuffer.</li>
839b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
840b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>When done, tell the Renderscript runtime stop rendering to the color buffer and back
841b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  to the default framebuffer by calling <code>rsgClearAllRenderTargets()</code>.</li>
842b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
843b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Create a fragment shader and bind a the color buffer to it as a texture.</li>
844b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
845b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Render your scene to the default framebuffer. The texture will be used according
846b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  to the way you setup your fragment shader.</li>
847b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</ul>
848b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
849b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<p>The following example shows you how to render to a framebuffer object by modifying the
850b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<a href="{@docRoot}guide/resources/renderscript/Fountain/">Fountain</a> Renderscript sample. The end
851b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyresult is the <a href="{@docRoot}guide/resources/renderscript/FountainFBO/">FountainFBO</a> sample.
852b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LyThe modifications render the exact same scene into a framebuffer object as it does the default
853b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyframebuffer. The framebuffer object is then rendered into the default framebuffer in a small
854b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyarea at the top left corner of the screen.</p>
855b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
856b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<ol>
857b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Modify <code>fountain.rs</code> and add the following global variables. This creates setter
858b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  methods when this file is reflected into a <code>.java</code> file, allowing you to allocate
859b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  memory in your Android framework code and binding it to the Renderscript runtime.
860b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<pre>
861b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly//allocation for color buffer
862b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyrs_allocation gColorBuffer;
863b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly//fragment shader for rendering without a texture (used for rendering to framebuffer object)
864b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyrs_program_fragment gProgramFragment;
865b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly//fragment shader for rendering with a texture (used for rendering to default framebuffer)
866b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyrs_program_fragment gTextureProgramFragment;
867b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</pre>
868b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </li>
869b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
870b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Modify the root function of <code>fountain.rs</code> to look like the following code. The
871b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  modifications are commented:
872b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<pre>
873b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyint root() {
874b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float dt = min(rsGetDt(), 0.1f);
875b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgClearColor(0.f, 0.f, 0.f, 1.f);
876b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    const float height = rsgGetHeight();
877b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    const int size = rsAllocationGetDimX(rsGetAllocation(point));
878b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float dy2 = dt * (10.f);
879b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Point_t * p = point;
880b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    for (int ct=0; ct < size; ct++) {
881b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p->delta.y += dy2;
882b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p->position += p->delta;
883b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        if ((p->position.y > height) && (p->delta.y > 0)) {
884b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly            p->delta.y *= -0.3f;
885b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        }
886b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        p++;
887b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    }
888b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Tell Renderscript runtime to render to the frame buffer object
889b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgBindColorTarget(gColorBuffer, 0);
890b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Begin rendering on a white background
891b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgClearColor(1.f, 1.f, 1.f, 1.f);
892b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgDrawMesh(partMesh);
893b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
894b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //When done, tell Renderscript runtime to stop rendering to framebuffer object
895b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgClearAllRenderTargets();
896b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
897b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Bind a new fragment shader that declares the framebuffer object to be used as a texture
898b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgBindProgramFragment(gTextureProgramFragment);
899b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
900b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Bind the framebuffer object to the fragment shader at slot 0 as a texture
901b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgBindTexture(gTextureProgramFragment, 0, gColorBuffer);
902b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Draw a quad using the framebuffer object as the texture
903b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float startX = 10, startY = 10;
904b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    float s = 256;
905b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgDrawQuadTexCoords(startX, startY, 0, 0, 1,
906b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                         startX, startY + s, 0, 0, 0,
907b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                         startX + s, startY + s, 0, 1, 0,
908b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly                         startX + s, startY, 0, 1, 1);
909b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
910b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Rebind the original fragment shader to render as normal
911b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgBindProgramFragment(gProgramFragment);
912b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
913b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    //Render the main scene
914b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    rsgDrawMesh(partMesh);
915b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
916b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    return 1;
917b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly}
918b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</pre>
919b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  </li>
920b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
921b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>In the <code>FountainRS.java</code> file, modify the <code>init()</code> method to look
922b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  like the following code. The modifications are commented:
923b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
924b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<pre>
925b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly/* Add necessary members */
926b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyprivate ScriptC_fountainfbo mScript;
927b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyprivate Allocation mColorBuffer;
928b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyprivate ProgramFragment mProgramFragment;
929b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyprivate ProgramFragment mTextureProgramFragment;
930b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
931b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lypublic void init(RenderScriptGL rs, Resources res) {
932b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mRS = rs;
933b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mRes = res;
934b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
935b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT);
936b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
937b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
938b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    smb.addVertexAllocation(points.getAllocation());
939b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    smb.addIndexSetType(Mesh.Primitive.POINT);
940b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Mesh sm = smb.create();
941b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
942b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript = new ScriptC_fountainfbo(mRS, mRes, R.raw.fountainfbo);
943b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript.set_partMesh(sm);
944b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript.bind_point(points);
945b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
946b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
947b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    pfb.setVaryingColor(true);
948b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mProgramFragment = pfb.create();
949b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript.set_gProgramFragment(mProgramFragment);
950b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
951b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    /* Second fragment shader to use a texture (framebuffer object) to draw with */
952b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
953b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly        ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
954b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
955b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    /* Set the fragment shader in the Renderscript runtime */
956b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mTextureProgramFragment = pfb.create();
957b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript.set_gTextureProgramFragment(mTextureProgramFragment);
958b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
959b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    /* Create the allocation for the color buffer */
960b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Type.Builder colorBuilder = new Type.Builder(mRS, Element.RGBA_8888(mRS));
961b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    colorBuilder.setX(256).setY(256);
962b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mColorBuffer = Allocation.createTyped(mRS, colorBuilder.create(),
963b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Allocation.USAGE_GRAPHICS_TEXTURE |
964b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    Allocation.USAGE_GRAPHICS_RENDER_TARGET);
965b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
966b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    /* Set the allocation in the Renderscript runtime */
967b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mScript.set_gColorBuffer(mColorBuffer);
968b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
969b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    mRS.bindRootScript(mScript);
970b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly}
971b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</pre>
972b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
973b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<p class="note"><strong>Note:</strong> This sample doesn't use a depth buffer, but the following code
974b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyshows you how to declare an example depth buffer if you need to use
975b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyone for your application. The depth buffer must have the same dimensions as the color buffer:
976b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
977b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly<pre>
978b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LyAllocation mDepthBuffer;
979b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
980b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly...
981b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
982b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LyType.Builder b = new Type.Builder(mRS, Element.createPixel(mRS, DataType.UNSIGNED_16,
983b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly    DataKind.PIXEL_DEPTH));
984b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Lyb.setX(256).setY(256);
985b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LymDepthBuffer = Allocation.createTyped(mRS, b.create(),
986b078d942911d5e1cf9533f7fe3e156c579ba396eRobert LyAllocation.USAGE_GRAPHICS_RENDER_TARGET);
987b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
988b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</pre>
989b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</p>
990b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</li>
991b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly
992b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  <li>Run and use the sample. The smaller, white quad on the top-left corner is using the
993b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly  framebuffer object as a texture, which renders the same scene as the main rendering.</li>
994b078d942911d5e1cf9533f7fe3e156c579ba396eRobert Ly</ol>
995