Home | History | Annotate | Download | only in renderscript
      1 page.title=3D Graphics
      2 parent.title=RenderScript 
      3 parent.link=index.html
      4 @jd:body
      5 
      6   <div id="qv-wrapper">
      7     <div id="qv">
      8       <h2>In this document</h2>
      9 
     10       <ol>
     11         <li>
     12           <a href="#creating-graphics-rs">Creating a Graphics Renderscript</a>
     13           <ol>
     14             <li><a href="#creating-native">Creating the native Renderscript file</a></li>
     15             <li><a href="#creating-entry">Creating the Renderscript entry point class</a></li>
     16             <li><a href="#creating-view">Creating the surface view</a></li>
     17             <li><a href="#creating-activity">Creating the activity</a></li>
     18           </ol>
     19         </li>
     20         <li>
     21           <a href="#drawing">Drawing</a>
     22           <ol>
     23             <li><a href="#drawing-rsg">Drawing using the rsgDraw functions</a></li>
     24             <li><a href="#drawing-mesh">Drawing with a mesh</a></li>
     25           </ol>
     26         </li>
     27         <li>
     28           <a href="#shaders">Shaders</a>
     29           <ol>
     30             <li><a href="#shader-bindings">Shader bindings</a></li>
     31             <li><a href="#shader-sampler">Defining a sampler</a></li>
     32           </ol>
     33         </li>
     34       </ol>
     35 
     36       <h2>Related Samples</h2>
     37 
     38       <ol>
     39         <li><a href="{@docRoot}resources/samples/RenderScript/Balls/index.html">Balls</a></li>
     40 
     41         <li><a href="{@docRoot}resources/samples/RenderScript/Fountain/index.html">Fountain</a></li>
     42 
     43         <li><a href="{@docRoot}resources/samples/RenderScript/HelloWorld/index.html">Hello
     44         World</a></li>
     45 
     46         <li><a
     47 href="{@docRoot}resources/samples/RenderScript/MiscSamples/index.html">Misc Samples</a></li>
     48       </ol>
     49     </div>
     50   </div>
     51 
     52   <p>RenderScript provides a number of graphics APIs for 3D rendering, both at the Android
     53   framework level as well as at the native level. For instance, the Android framework APIs let you
     54   create meshes and define shaders to customize the graphical rendering pipeline. The native
     55   RenderScript graphics APIs lets you draw the actual meshes to render your scene. In general, you
     56   will need to be familiar with APIs to appropriately render 3D graphics on an Android-powered
     57   device.</p>
     58 
     59   <h2 id="creating-graphics-rs">Creating a Graphics RenderScript</h2>
     60 
     61   <p>Because of the various layers of code when writing a RenderScript application, it is useful to
     62   create the following files for a scene that you want to render:</p>
     63 
     64   <ul>
     65     <li>The native RenderScript <code>.rs</code> file. This file contains the logic to do the
     66     graphics rendering.</li>
     67 
     68     <li>The RenderScript entry point class that allows your view to interact with the code defined
     69     in the <code>.rs</code> file. This class contains a RenderScript object(instance of
     70     <code>ScriptC_<em>renderscript_file</em></code>), which allows your Android framework code to
     71     call the native RenderScript code. This class also creates the {@link
     72     android.renderscript.RenderScriptGL} context object, which contains the current rendering state
     73     of the RenderScript such as programs (vertex and fragment shaders, for example) that you want
     74     to define and bind to the graphics pipeline. The context object attaches to the RenderScript
     75     object (instance of <code><em>ScriptC_renderscript_file</em></code>) that does the rendering.
     76     Our example names this class <code>HelloWorldRS</code>.</li>
     77 
     78     <li>Create a class that extends {@link android.renderscript.RSSurfaceView} to provide a surface
     79     to render on. If you want to implement callbacks from events inherited from {@link
     80     android.view.View}, such as {@link android.view.View#onTouchEvent onTouchEvent()} and {@link
     81     android.view.View#onKeyDown onKeyDown()}, do so in this class as well.</li>
     82 
     83     <li>Create a class that is the main Activity class, like you would with any Android
     84     application. This class sets your {@link android.renderscript.RSSurfaceView} as the content
     85     view for this Activity.</li>
     86   </ul>
     87 
     88   <p>The following sections describe how to implement these three classes by using the HelloWorld
     89   RenderScript sample that is provided in the SDK as a guide (some code has been modified from its
     90   original form for simplicity).</p>
     91 
     92   <h3 id="creating-native">Creating the native RenderScript file</h3>
     93 
     94   <p>Your native RenderScript code resides in a <code>.rs</code> file in the
     95   <code>&lt;project_root&gt;/src/</code> directory. You can also define <code>.rsh</code> header
     96   files. This code contains the logic to render your graphics and declares all necessary variables
     97   and pointers. Every graphics <code>.rs</code> file generally contains the following items:</p>
     98 
     99   <ul>
    100     <li>A pragma (<code>#pragma rs java_package_name(<em>package.name</em>)</code>) that declares
    101     the package name of the <code>.java</code> reflection of this RenderScript.</li>
    102 
    103     <li>A pragma (<code>#pragma version(1)</code>) that declares the version of RenderScript that
    104     you are using (1 is the only value for now).</li>
    105 
    106     <li>A <code>#include</code> of the rs_graphics.rsh header file.</li>
    107 
    108     <li>A <code>root()</code> function. This is the main worker function for your RenderScript and
    109     calls RenderScript graphics APIs to draw meshes to the surface. This function is called every
    110     time a frame refresh occurs, which is specified as its return value. A <code>0</code> specified
    111     for the return value says to only render the frame when a property of the scene that you are
    112     rendering changes. A non-zero positive integer specifies the refresh rate of the frame in
    113     milliseconds.
    114 
    115       <p class="note"><strong>Note:</strong> The RenderScript runtime makes its best effort to
    116       refresh the frame at the specified rate. For example, if you are creating a live wallpaper
    117       and set the return value to 50, the runtime renders the wallpaper at 20fps if it has just
    118       enough or more resources to do so, and renders as fast as it can if it does not.</p>
    119       
    120       <p>For more
    121       information on using the RenderScript graphics functions, see the <a href=
    122       "#drawing">Drawing</a> section.</p>
    123     </li>
    124 
    125     <li>An <code>init()</code> function. This allows you to do any initialization of your
    126     RenderScript before the <code>root()</code> function runs, such as initializing variables. This
    127     function runs once and is called automatically when the RenderScript starts, before anything
    128     else in your RenderScript. Creating this function is optional.</li>
    129 
    130     <li>Any variables, pointers, and structures that you wish to use in your RenderScript code (can
    131     be declared in <code>.rsh</code> files if desired)</li>
    132   </ul>
    133 
    134   <p>The following code shows how the <code>helloworld.rs</code> file is implemented:</p>
    135   <pre>
    136 #pragma version(1)
    137 
    138 // Tell which java package name the reflected files should belong to
    139 #pragma rs java_package_name(com.android.rs.helloworld)
    140 
    141 // Built-in header with graphics APIs
    142 #include "rs_graphics.rsh"
    143 
    144 // gTouchX and gTouchY are variables that are reflected for use
    145 // by the Android framework API. This RenderScript uses them to be notified of touch events.
    146 int gTouchX;
    147 int gTouchY;
    148 
    149 // This is invoked automatically when the script is created and initializes the variables
    150 // in the Android framework layer as well.
    151 void init() {
    152     gTouchX = 50.0f;
    153     gTouchY = 50.0f;
    154 }
    155 
    156 int root(int launchID) {
    157 
    158     // Clear the background color
    159     rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    160     // Tell the runtime what the font color should be
    161     rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
    162     // Introuduce ourselves to the world by drawing a greeting
    163     // at the position user touched on the screen
    164     rsgDrawText("Hello World!", gTouchX, gTouchY);
    165 
    166     // Return value tells RS roughly how often to redraw
    167     // in this case 20 ms
    168     return 20;
    169 }
    170 </pre>
    171 
    172   <h3 id="creating-entry">Creating the RenderScript entry point class</h3>
    173 
    174   <p>When you create a RenderScript (<code>.rs</code>) file, it is helpful to create a
    175   corresponding Android framework class that is an entry point into the <code>.rs</code> file. In
    176   this entry point class, you create a RenderScript object by instantiating a
    177   <code>ScriptC_<em>rs_filename</em></code> and binding it to the RenderScript context. The
    178   RenderScript object is attached to the RenderScript bytecode, which is platform-independent and
    179   gets compiled on the device when the RenderScript application runs. Both the
    180   <code>ScriptC_<em>rs_filename</em></code> class and bytecode is generated by the Android build
    181   tools and is packaged with the <code>.apk</code> file. The bytecode file is located in the
    182   <code>&lt;project_root&gt;/res/raw/</code> directory and is named <code>rs_filename.bc</code>.
    183   You refer to the bytecode as a resource (<code>R.raw.<em>rs_filename</em></code>). when creating
    184   the RenderScript object..</p>
    185 
    186   <p>You then bind the RenderScript object to the RenderScript context, so that the surface view
    187   knows what code to use to render graphics. The following code shows how the
    188   <code>HelloWorldRS</code> class is implemented:</p>
    189   <pre>
    190 package com.android.rs.helloworld;
    191 
    192 import android.content.res.Resources;
    193 import android.renderscript.*;
    194 
    195 public class HelloWorldRS {
    196     //context and resources are obtained from RSSurfaceView, which calls init()
    197     private Resources mRes;
    198     private RenderScriptGL mRS;
    199 
    200     //Declare the RenderScript object
    201     private ScriptC_helloworld mScript;
    202 
    203     public HelloWorldRS() {
    204     }
    205 
    206     /**
    207      * This provides us with the RenderScript context and resources
    208      * that allow us to create the RenderScript object
    209      */
    210     public void init(RenderScriptGL rs, Resources res) {
    211         mRS = rs;
    212         mRes = res;
    213         initRS();
    214     }
    215     /**
    216      * Calls native RenderScript functions (set_gTouchX and set_gTouchY)
    217      * through the reflected layer class ScriptC_helloworld to pass in
    218      * touch point data.
    219      */
    220     public void onActionDown(int x, int y) {
    221         mScript.set_gTouchX(x);
    222         mScript.set_gTouchY(y);
    223     }
    224     /**
    225      * Binds the RenderScript object to the RenderScript context
    226      */
    227     private void initRS() {
    228         //create the RenderScript object
    229         mScript = new ScriptC_helloworld(mRS, mRes, R.raw.helloworld);
    230         //bind the RenderScript object to the RenderScript context
    231         mRS.bindRootScript(mScript);
    232     }
    233 }
    234 
    235 </pre>
    236 
    237   <h3 id="creating-view">Creating the surface view</h3>
    238 
    239   <p>To create a surface view to render graphics on, create a class that extends {@link
    240   android.renderscript.RSSurfaceView}. This class also creates a RenderScript context object
    241   ({@link android.renderscript.RenderScriptGL} and passes it to the Rendscript entry point class to
    242   bind the two. The following code shows how the <code>HelloWorldView</code> class is
    243   implemented:</p>
    244   <pre>
    245 package com.android.rs.helloworld;
    246 
    247 import android.renderscript.RSSurfaceView;
    248 import android.renderscript.RenderScriptGL;
    249 import android.content.Context;
    250 import android.view.MotionEvent;
    251 
    252 public class HelloWorldView extends RSSurfaceView {
    253     // RenderScript context
    254     private RenderScriptGL mRS;
    255     // RenderScript entry point object that does the rendering
    256     private HelloWorldRS mRender;
    257 
    258     public HelloWorldView(Context context) {
    259         super(context);
    260         initRS();
    261     }
    262 
    263     private void initRS() {
    264         if (mRS == null) {
    265             // Initialize RenderScript with default surface characteristics.
    266             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
    267             //Create the RenderScript context
    268             mRS = createRenderScriptGL(sc);
    269             // Create an instance of the RenderScript entry point class
    270             mRender = new HelloWorldRS();
    271             // Call the entry point class to bind it to this context
    272             mRender.init(mRS, getResources());
    273         }
    274     }
    275 
    276     /**
    277      * Rebind everything when the window becomes attached
    278      */
    279     protected void onAttachedToWindow() {
    280         super.onAttachedToWindow();
    281         initRS();
    282     }
    283 
    284     /**
    285      * Stop rendering when window becomes detached
    286      */
    287     protected void onDetachedFromWindow() {
    288         // Handle the system event and clean up
    289         mRender = null;
    290         if (mRS != null) {
    291             mRS = null;
    292             destroyRenderScriptGL();
    293         }
    294     }
    295 
    296     /**
    297      * Use callbacks to relay data to RenderScript entry point class
    298      */
    299     public boolean onTouchEvent(MotionEvent ev) {
    300         // Pass touch events from the system to the rendering script
    301         if (ev.getAction() == MotionEvent.ACTION_DOWN) {
    302             mRender.onActionDown((int)ev.getX(), (int)ev.getY());
    303             return true;
    304         }
    305 
    306         return false;
    307     }
    308 }
    309 
    310 </pre>
    311 
    312   <h3 id="creating-activity">Creating the Activity</h3>
    313 
    314   <p>Applications that use RenderScript still adhere to activity lifecyle, and are part of the same
    315   view hierarchy as traditional Android applications, which is handled by the Android VM. This
    316   Activity class sets its view to be the {@link android.renderscript.RSSurfaceView} and handles
    317   lifecycle callback events appropriately. The following code shows how the <code>HelloWorld</code>
    318   class is implemented:</p>
    319   <pre>
    320 public class HelloWorldActivity extends Activity {
    321 
    322     //Custom view to use with RenderScript
    323     private HelloWorldView view;
    324 
    325     public void onCreate(Bundle icicle) {
    326         super.onCreate(icicle);
    327         // Create surface view and set it as the content of our Activity
    328         mView = new HelloWorldView(this);
    329         setContentView(view);
    330     }
    331 
    332     protected void onResume() {
    333         // Ideally an app should implement onResume() and onPause()
    334         // to take appropriate action when the activity loses focus
    335         super.onResume();
    336         view.resume();
    337     }
    338 
    339     protected void onPause() {
    340         // Ideally an app should implement onResume() and onPause()
    341         // to take appropriate action when the activity loses focus
    342         super.onPause();
    343         view.pause();
    344     }
    345 }
    346 </pre>
    347 
    348   <h2 id="drawing">Drawing</h2>
    349   <p>The following sections describe how to use the graphics functions to draw with Renderscript.</p>
    350   <h3 id="drawing-rsg">Drawing using the rsgDraw functions</h3>
    351 
    352   <p>The native RenderScript APIs provide a few convenient functions to easily draw a polygon to
    353   the screen. You call these in your <code>root()</code> function to have them render to the
    354   surface view. These functions are available for simple drawing and should not be used for complex
    355   graphics rendering:</p>
    356 
    357   <ul>
    358     <li><code>rsgDrawRect()</code>: Sets up a mesh and draws a rectangle to the screen. It uses the
    359     top left vertex and bottom right vertex of the rectangle to draw.</li>
    360 
    361     <li><code>rsgDrawQuad()</code>: Sets up a mesh and draws a quadrilateral to the screen.</li>
    362 
    363     <li><code>rsgDrawQuadTexCoords()</code>: Sets up a mesh and draws a textured quadrilateral to
    364     the screen.</li>
    365   </ul>
    366 
    367   <h3 id="drawing-mesh">Drawing with a mesh</h3>
    368 
    369   <p>When you want to draw complex shapes and textures to the screen, instantiate a {@link
    370   android.renderscript.Mesh} and draw it to the screen with <code>rsgDrawMesh()</code>. A {@link
    371   android.renderscript.Mesh} is a collection of allocations that represent vertex data (positions,
    372   normals, texture coordinates) and index data such as triangles and lines. You can build a Mesh in
    373   three different ways:</p>
    374 
    375   <ul>
    376     <li>Build the mesh with the {@link android.renderscript.Mesh.TriangleMeshBuilder} class, which
    377     allows you to specify a set of vertices and indices for each triangle that you want to draw.
    378     The downside of doing it this way is there is no way to specify the vertices in your native
    379     RenderScript code.</li>
    380 
    381     <li>Build the mesh using an {@link android.renderscript.Allocation} or a set of {@link
    382     android.renderscript.Allocation}s with the {@link android.renderscript.Mesh.AllocationBuilder}
    383     class. This allows you to build a mesh with vertices already stored in memory, which allows you
    384     to set the vertices in native or Android code.</li>
    385 
    386     <li>Build the mesh with the {@link android.renderscript.Mesh.Builder} class. This is a
    387     convenience method for when you know what data types you want to use to build your mesh, but
    388     don't want to make separate memory allocations like with {@link
    389     android.renderscript.Mesh.AllocationBuilder}. You can specify the types that you want and this
    390     mesh builder automatically creates the memory allocations for you.</li>
    391   </ul>
    392 
    393   <p>To create a mesh using the {@link android.renderscript.Mesh.TriangleMeshBuilder}, you need to
    394   supply it with a set of vertices and the indices for the vertices that comprise the triangle. For
    395   example, the following code specifies three vertices, which are added to an internal array,
    396   indexed in the order they were added. The call to {@link
    397   android.renderscript.Mesh.TriangleMeshBuilder#addTriangle addTriangle()} draws the triangle with
    398   vertex 0, 1, and 2 (the vertices are drawn counter-clockwise).</p>
    399   <pre>
    400 int float2VtxSize = 2;
    401 Mesh.TriangleMeshBuilder triangle = new Mesh.TriangleMeshBuilder(renderscriptGL,
    402 float2VtxSize, Mesh.TriangleMeshBuilder.COLOR);
    403 triangles.addVertex(300.f, 300.f);
    404 triangles.addVertex(150.f, 450.f);
    405 triangles.addVertex(450.f, 450.f);
    406 triangles.addTriangle(0 , 1, 2);
    407 Mesh smP = triangle.create(true);
    408 script.set_mesh(smP);
    409 </pre>
    410 
    411   <p>To draw a mesh using the {@link android.renderscript.Mesh.AllocationBuilder}, you need to
    412   supply it with one or more allocations that contain the vertex data:</p>
    413   <pre>
    414 Allocation vertices;
    415 
    416 ...
    417 Mesh.AllocationBuilder triangle = new Mesh.AllocationBuilder(mRS);
    418 smb.addVertexAllocation(vertices.getAllocation());
    419 smb.addIndexSetType(Mesh.Primitive.TRIANGLE);
    420 Mesh smP = smb.create();
    421 script.set_mesh(smP);
    422 </pre>
    423 
    424   <p>In your native RenderScript code, draw the built mesh to the screen:</p>
    425   <pre>
    426 rs_mesh mesh;
    427 ...
    428 
    429 int root(){
    430 ...
    431 rsgDrawMesh(mesh);
    432 ...
    433 return 0; //specify a non zero, positive integer to specify the frame refresh.
    434           //0 refreshes the frame only when the mesh changes.
    435 }
    436 </pre>
    437 
    438   <h2 id="shaders">Shaders</h2>
    439 
    440   <p>You can attach four program objects to the {@link android.renderscript.RenderScriptGL} context
    441   to customize the rendering pipeline. For example, you can create vertex and fragment shaders in
    442   GLSL or build a raster program object with provided methods without writing GLSL code. The four
    443   program objects mirror a traditional graphical rendering pipeline:</p>
    444 
    445   <table>
    446     <tr>
    447       <th>Android Object Type</th>
    448 
    449       <th>RenderScript Native Type</th>
    450 
    451       <th>Description</th>
    452     </tr>
    453 
    454     <tr>
    455       <td>{@link android.renderscript.ProgramVertex}</td>
    456 
    457       <td>rs_program_vertex</td>
    458 
    459       <td>
    460         <p>The RenderScript vertex program, also known as a vertex shader, describes the stage in
    461         the graphics pipeline responsible for manipulating geometric data in a user-defined way.
    462         The object is constructed by providing RenderScript with the following data:</p>
    463 
    464         <ul>
    465           <li>An Element describing its varying inputs or attributes</li>
    466 
    467           <li>GLSL shader string that defines the body of the program</li>
    468 
    469           <li>a Type that describes the layout of an Allocation containing constant or uniform
    470           inputs</li>
    471         </ul>
    472 
    473         <p>Once the program is created, bind it to the {@link android.renderscript.RenderScriptGL}
    474         graphics context by calling {@link android.renderscript.RenderScriptGL#bindProgramVertex
    475         bindProgramVertex()}. It is then used for all subsequent draw calls until you bind a new
    476         program. If the program has constant inputs, the user needs to bind an allocation
    477         containing those inputs. The allocation's type must match the one provided during creation.
    478         The RenderScript library then does all the necessary plumbing to send those constants to
    479         the graphics hardware. Varying inputs to the shader, such as position, normal, and texture
    480         coordinates are matched by name between the input Element and the Mesh object being drawn.
    481         The signatures don't have to be exact or in any strict order. As long as the input name in
    482         the shader matches a channel name and size available on the mesh, the run-time would take
    483         care of connecting the two. Unlike OpenGL, there is no need to link the vertex and fragment
    484         programs.</p>
    485 
    486         <p>To bind shader constructs to the Program, declare a struct containing the necessary
    487         shader constants in your native RenderScript code. This struct is generated into a
    488         reflected class that you can use as a constant input element during the Program's creation.
    489         It is an easy way to create an instance of this struct as an allocation. You would then
    490         bind this Allocation to the Program and the RenderScript system sends the data that is
    491         contained in the struct to the hardware when necessary. To update shader constants, you
    492         change the values in the Allocation and notify the native RenderScript code of the
    493         change.</p>
    494       </td>
    495     </tr>
    496 
    497     <tr>
    498       <td>{@link android.renderscript.ProgramFragment}</td>
    499 
    500       <td>rs_program_fragment</td>
    501 
    502       <td>
    503         <p>The RenderScript fragment program, also known as the fragment shader, is responsible for
    504         manipulating pixel data in a user-defined way. It's constructed from a GLSL shader string
    505         containing the program body, textures inputs, and a Type object describing the constants
    506         used by the program. Like the vertex programs, when an allocation with constant input
    507         values is bound to the shader, its values are sent to the graphics program automatically.
    508         Note that the values inside the allocation are not explicitly tracked. If they change
    509         between two draw calls using the same program object, notify the runtime of that change by
    510         calling rsgAllocationSyncAll so it could send the new values to hardware. Communication
    511         between the vertex and fragment programs is handled internally in the GLSL code. For
    512         example, if the fragment program is expecting a varying input called varTex0, the GLSL code
    513         inside the program vertex must provide it.</p>
    514 
    515         <p>To bind shader constants to this program, declare a struct containing the necessary
    516         shader constants in your native RenderScript code. This struct is generated into a
    517         reflected class that you can use as a constant input element during the Program's creation.
    518         It is an easy way to create an instance of this struct as an allocation. You would then
    519         bind this Allocation to the Program and the RenderScript system sends the data that is
    520         contained in the struct to the hardware when necessary. To update shader constants, you
    521         change the values in the Allocation and notify the native RenderScript code of the
    522         change.</p>
    523       </td>
    524     </tr>
    525 
    526     <tr>
    527       <td>{@link android.renderscript.ProgramStore}</td>
    528 
    529       <td>rs_program_store</td>
    530 
    531       <td>The RenderScript ProgramStore contains a set of parameters that control how the graphics
    532       hardware writes to the framebuffer. It could be used to enable and disable depth writes and
    533       testing, setup various blending modes for effects like transparency and define write masks
    534       for color components.</td>
    535     </tr>
    536 
    537     <tr>
    538       <td>{@link android.renderscript.ProgramRaster}</td>
    539 
    540       <td>rs_program_raster</td>
    541 
    542       <td>Program raster is primarily used to specify whether point sprites are enabled and to
    543       control the culling mode. By default back faces are culled.</td>
    544     </tr>
    545   </table>
    546 
    547   <p>The following example defines a vertex shader in GLSL and binds it to the RenderScript:</p>
    548   <pre>
    549     private RenderScriptGL glRenderer;      //rendering context
    550     private ScriptField_Point mPoints;      //vertices
    551     private ScriptField_VpConsts mVpConsts; //shader constants
    552 
    553     ...
    554 
    555      ProgramVertex.Builder sb = new ProgramVertex.Builder(glRenderer);
    556         String t =  "varying vec4 varColor;\n" +
    557                     "void main() {\n" +
    558                     "  vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n" +
    559                     "  pos.xy = ATTRIB_position;\n" +
    560                     "  gl_Position = UNI_MVP * pos;\n" +
    561                     "  varColor = vec4(1.0, 1.0, 1.0, 1.0);\n" +
    562                     "  gl_PointSize = ATTRIB_size;\n" +
    563                     "}\n";
    564         sb.setShader(t);
    565         sb.addConstant(mVpConsts.getType());
    566         sb.addInput(mPoints.getElement());
    567         ProgramVertex pvs = sb.create();
    568         pvs.bindConstants(mVpConsts.getAllocation(), 0);
    569         glRenderer.bindProgramVertex(pvs);
    570 
    571 
    572 </pre>
    573 
    574   <p>The <a href=
    575   "{@docRoot}resources/samples/RenderScript/MiscSamples/src/com/example/android/rs/miscsamples/RsRenderStatesRS.html">
    576   RsRenderStatesRS</a> sample has many examples on how to create a shader without writing GLSL.</p>
    577 
    578   <h3 id="shader-bindings">Shader bindings</h3>
    579 
    580   <p>You can also set four pragmas that control the shaders' default bindings to the {@link
    581   android.renderscript.RenderScriptGL} context when the script is executing:</p>
    582 
    583   <ul>
    584     <li>stateVertex</li>
    585 
    586     <li>stateFragment</li>
    587 
    588     <li>stateRaster</li>
    589 
    590     <li>stateStore</li>
    591   </ul>
    592 
    593   <p>The possible values for each pragma are <code>parent</code> or <code>default</code>. Using
    594   <code>default</code> binds the shaders to the graphical context with the system defaults. The
    595   default shader is defined below:</p>
    596   <pre>
    597 ("varying vec4 varColor;\n");
    598 ("varying vec2 varTex0;\n");
    599 ("void main() {\n");
    600 (" gl_Position = UNI_MVP * ATTRIB_position;\n");
    601 (" gl_PointSize = 1.0;\n");
    602 (" varColor = ATTRIB_color;\n");
    603 (" varTex0 = ATTRIB_texture0;\n");
    604 ("}\n");
    605 </pre>
    606 
    607   <p>Using <code>parent</code> binds the shaders in the same manner as it is bound in the calling
    608   script. If this is the root script, the parent state is taken from the bind points that are set
    609   by the {@link android.renderscript.RenderScriptGL} bind methods.</p>
    610 
    611   <p>For example, you can define this at the top of your native graphics RenderScript code to have
    612   the Vertex and Store shaders inherent the bind properties from their parent scripts:</p>
    613   <pre>
    614 #pragma stateVertex(parent)
    615 #pragma stateStore(parent)
    616 </pre>
    617 
    618   <h3 id="shader-sampler">Defining a sampler</h3>
    619 
    620   <p>A {@link android.renderscript.Sampler} object defines how data is extracted from textures.
    621   Samplers are bound to Program objects (currently only a Fragment Program) alongside the texture
    622   whose sampling they control. These objects are used to specify such things as edge clamping
    623   behavior, whether mip-maps are used, and the amount of anisotropy required. There might be
    624   situations where hardware does not support the desired behavior of the sampler. In these cases,
    625   the runtime attempts to provide the closest possible approximation. For example, the user
    626   requested 16x anisotropy, but only 8x was set because it's the best available on the
    627   hardware.</p>
    628 
    629   <p>The <a href=
    630   "{@docRoot}resources/samples/RenderScript/MiscSamples/src/com/example/android/rs/miscsamples/RsRenderStatesRS.html">
    631   RsRenderStatesRS</a> sample has many examples on how to create a sampler and bind it to a
    632   Fragment program.</p>
    633   
    634 </body>
    635 </html>
    636