rs_graphics.rsh revision 25a59d051a218bd608b005a149509464b3e6d4f0
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** @file rs_graphics.rsh
18 *  \brief Renderscript graphics API
19 *
20 *  A set of graphics functions used by Renderscript.
21 *
22 */
23#ifndef __RS_GRAPHICS_RSH__
24#define __RS_GRAPHICS_RSH__
25
26// These are API 15 once it get official
27typedef enum {
28    RS_DEPTH_FUNC_ALWAYS,
29    RS_DEPTH_FUNC_LESS,
30    RS_DEPTH_FUNC_LEQUAL,
31    RS_DEPTH_FUNC_GREATER,
32    RS_DEPTH_FUNC_GEQUAL,
33    RS_DEPTH_FUNC_EQUAL,
34    RS_DEPTH_FUNC_NOTEQUAL,
35
36    RS_DEPTH_FUNC_INVALID = 100,
37} rs_depth_func;
38
39typedef enum {
40    RS_BLEND_SRC_ZERO,                  // 0
41    RS_BLEND_SRC_ONE,                   // 1
42    RS_BLEND_SRC_DST_COLOR,             // 2
43    RS_BLEND_SRC_ONE_MINUS_DST_COLOR,   // 3
44    RS_BLEND_SRC_SRC_ALPHA,             // 4
45    RS_BLEND_SRC_ONE_MINUS_SRC_ALPHA,   // 5
46    RS_BLEND_SRC_DST_ALPHA,             // 6
47    RS_BLEND_SRC_ONE_MINUS_DST_ALPHA,   // 7
48    RS_BLEND_SRC_SRC_ALPHA_SATURATE,    // 8
49
50    RS_BLEND_SRC_INVALID = 100,
51} rs_blend_src_func;
52
53typedef enum {
54    RS_BLEND_DST_ZERO,                  // 0
55    RS_BLEND_DST_ONE,                   // 1
56    RS_BLEND_DST_SRC_COLOR,             // 2
57    RS_BLEND_DST_ONE_MINUS_SRC_COLOR,   // 3
58    RS_BLEND_DST_SRC_ALPHA,             // 4
59    RS_BLEND_DST_ONE_MINUS_SRC_ALPHA,   // 5
60    RS_BLEND_DST_DST_ALPHA,             // 6
61    RS_BLEND_DST_ONE_MINUS_DST_ALPHA,   // 7
62
63    RS_BLEND_DST_INVALID = 100,
64} rs_blend_dst_func;
65
66typedef enum {
67    RS_CULL_BACK,
68    RS_CULL_FRONT,
69    RS_CULL_NONE,
70
71    RS_CULL_INVALID = 100,
72} rs_cull_mode;
73
74typedef enum {
75    RS_SAMPLER_NEAREST,
76    RS_SAMPLER_LINEAR,
77    RS_SAMPLER_LINEAR_MIP_LINEAR,
78    RS_SAMPLER_WRAP,
79    RS_SAMPLER_CLAMP,
80    RS_SAMPLER_LINEAR_MIP_NEAREST,
81
82    RS_SAMPLER_INVALID = 100,
83} rs_sampler_value;
84
85#if (defined(RS_VERSION) && (RS_VERSION >= 14))
86/**
87 * Set the color target used for all subsequent rendering calls
88 * @param colorTarget
89 * @param slot
90 */
91extern void __attribute__((overloadable))
92    rsgBindColorTarget(rs_allocation colorTarget, uint slot);
93
94/**
95 * Clear the previously set color target
96 * @param slot
97 */
98extern void __attribute__((overloadable))
99    rsgClearColorTarget(uint slot);
100
101/**
102 * Set the depth target used for all subsequent rendering calls
103 * @param depthTarget
104 */
105extern void __attribute__((overloadable))
106    rsgBindDepthTarget(rs_allocation depthTarget);
107
108/**
109 * Clear the previously set depth target
110 */
111extern void __attribute__((overloadable))
112    rsgClearDepthTarget(void);
113
114/**
115 * Clear all color and depth targets and resume rendering into
116 * the framebuffer
117 */
118extern void __attribute__((overloadable))
119    rsgClearAllRenderTargets(void);
120
121/**
122 * Force Renderscript to finish all rendering commands
123 */
124extern uint __attribute__((overloadable))
125    rsgFinish(void);
126
127#endif //defined(RS_VERSION) && (RS_VERSION >= 14)
128
129/**
130 * Bind a new ProgramFragment to the rendering context.
131 *
132 * @param pf
133 */
134extern void __attribute__((overloadable))
135    rsgBindProgramFragment(rs_program_fragment pf);
136
137/**
138 * Bind a new ProgramStore to the rendering context.
139 *
140 * @param ps
141 */
142extern void __attribute__((overloadable))
143    rsgBindProgramStore(rs_program_store ps);
144
145
146/**
147 * @hide
148 * Get program store depth function
149 *
150 * @param ps
151 */
152extern rs_depth_func __attribute__((overloadable))
153    rsgProgramStoreGetDepthFunc(rs_program_store ps);
154
155/**
156 * @hide
157 * Get program store depth mask
158 *
159 * @param ps
160 */
161extern bool __attribute__((overloadable))
162    rsgProgramStoreGetDepthMask(rs_program_store ps);
163/**
164 * @hide
165 * Get program store red component color mask
166 *
167 * @param ps
168 */
169extern bool __attribute__((overloadable))
170    rsgProgramStoreGetColorMaskR(rs_program_store ps);
171
172/**
173 * @hide
174 * Get program store green component color mask
175 *
176 * @param ps
177 */
178extern bool __attribute__((overloadable))
179    rsgProgramStoreGetColorMaskG(rs_program_store ps);
180
181/**
182 * @hide
183 * Get program store blur component color mask
184 *
185 * @param ps
186 */
187extern bool __attribute__((overloadable))
188    rsgProgramStoreGetColorMaskB(rs_program_store ps);
189
190/**
191 * @hide
192 * Get program store alpha component color mask
193 *
194 * @param ps
195 */
196extern bool __attribute__((overloadable))
197    rsgProgramStoreGetColorMaskA(rs_program_store ps);
198
199/**
200 * @hide
201 * Get program store blend source function
202 *
203 * @param ps
204 */
205extern rs_blend_src_func __attribute__((overloadable))
206        rsgProgramStoreGetBlendSrcFunc(rs_program_store ps);
207
208/**
209 * @hide
210 * Get program store blend destination function
211 *
212 * @param ps
213 */
214extern rs_blend_dst_func __attribute__((overloadable))
215    rsgProgramStoreGetBlendDstFunc(rs_program_store ps);
216
217/**
218 * @hide
219 * Get program store dither state
220 *
221 * @param ps
222 */
223extern bool __attribute__((overloadable))
224    rsgProgramStoreGetDitherEnabled(rs_program_store ps);
225
226
227/**
228 * Bind a new ProgramVertex to the rendering context.
229 *
230 * @param pv
231 */
232extern void __attribute__((overloadable))
233    rsgBindProgramVertex(rs_program_vertex pv);
234
235/**
236 * Bind a new ProgramRaster to the rendering context.
237 *
238 * @param pr
239 */
240extern void __attribute__((overloadable))
241    rsgBindProgramRaster(rs_program_raster pr);
242
243/**
244 * @hide
245 * Get program raster point sprite state
246 *
247 * @param pr
248 */
249extern bool __attribute__((overloadable))
250    rsgProgramRasterGetPointSpriteEnabled(rs_program_raster pr);
251
252/**
253 * @hide
254 * Get program raster cull mode
255 *
256 * @param pr
257 */
258extern rs_cull_mode __attribute__((overloadable))
259    rsgProgramRasterGetCullMode(rs_program_raster pr);
260
261/**
262 * Bind a new Sampler object to a ProgramFragment.  The sampler will
263 * operate on the texture bound at the matching slot.
264 *
265 * @param slot
266 */
267extern void __attribute__((overloadable))
268    rsgBindSampler(rs_program_fragment, uint slot, rs_sampler);
269
270/**
271 * @hide
272 * Get sampler minification value
273 *
274 * @param pr
275 */
276extern rs_sampler_value __attribute__((overloadable))
277    rsgSamplerGetMinification(rs_sampler s);
278
279/**
280 * @hide
281 * Get sampler magnification value
282 *
283 * @param pr
284 */
285extern rs_sampler_value __attribute__((overloadable))
286    rsgSamplerGetMagnification(rs_sampler s);
287
288/**
289 * @hide
290 * Get sampler wrap S value
291 *
292 * @param pr
293 */
294extern rs_sampler_value __attribute__((overloadable))
295    rsgSamplerGetWrapS(rs_sampler s);
296
297/**
298 * @hide
299 * Get sampler wrap T value
300 *
301 * @param pr
302 */
303extern rs_sampler_value __attribute__((overloadable))
304    rsgSamplerGetWrapT(rs_sampler s);
305
306/**
307 * @hide
308 * Get sampler anisotropy
309 *
310 * @param pr
311 */
312extern float __attribute__((overloadable))
313    rsgSamplerGetAnisotropy(rs_sampler s);
314
315/**
316 * Bind a new Allocation object to a ProgramFragment.  The
317 * Allocation must be a valid texture for the Program.  The sampling
318 * of the texture will be controled by the Sampler bound at the
319 * matching slot.
320 *
321 * @param slot
322 */
323extern void __attribute__((overloadable))
324    rsgBindTexture(rs_program_fragment, uint slot, rs_allocation);
325
326/**
327 * Load the projection matrix for a currently bound fixed function
328 * vertex program. Calling this function with a custom vertex shader
329 * would result in an error.
330 * @param proj projection matrix
331 */
332extern void __attribute__((overloadable))
333    rsgProgramVertexLoadProjectionMatrix(const rs_matrix4x4 *proj);
334/**
335 * Load the model matrix for a currently bound fixed function
336 * vertex program. Calling this function with a custom vertex shader
337 * would result in an error.
338 * @param model model matrix
339 */
340extern void __attribute__((overloadable))
341    rsgProgramVertexLoadModelMatrix(const rs_matrix4x4 *model);
342/**
343 * Load the texture matrix for a currently bound fixed function
344 * vertex program. Calling this function with a custom vertex shader
345 * would result in an error.
346 * @param tex texture matrix
347 */
348extern void __attribute__((overloadable))
349    rsgProgramVertexLoadTextureMatrix(const rs_matrix4x4 *tex);
350/**
351 * Get the projection matrix for a currently bound fixed function
352 * vertex program. Calling this function with a custom vertex shader
353 * would result in an error.
354 * @param proj matrix to store the current projection matrix into
355 */
356extern void __attribute__((overloadable))
357    rsgProgramVertexGetProjectionMatrix(rs_matrix4x4 *proj);
358
359/**
360 * Set the constant color for a fixed function emulation program.
361 *
362 * @param pf
363 * @param r
364 * @param g
365 * @param b
366 * @param a
367 */
368extern void __attribute__((overloadable))
369    rsgProgramFragmentConstantColor(rs_program_fragment pf, float r, float g, float b, float a);
370
371/**
372 * Get the width of the current rendering surface.
373 *
374 * @return uint
375 */
376extern uint __attribute__((overloadable))
377    rsgGetWidth(void);
378
379/**
380 * Get the height of the current rendering surface.
381 *
382 * @return uint
383 */
384extern uint __attribute__((overloadable))
385    rsgGetHeight(void);
386
387
388/**
389 * Sync the contents of an allocation from its SCRIPT memory space to its HW
390 * memory spaces.
391 *
392 * @param alloc
393 */
394extern void __attribute__((overloadable))
395    rsgAllocationSyncAll(rs_allocation alloc);
396
397#if (defined(RS_VERSION) && (RS_VERSION >= 14))
398
399/**
400 * Sync the contents of an allocation from memory space
401 * specified by source.
402 *
403 * @param alloc
404 * @param source
405 */
406extern void __attribute__((overloadable))
407    rsgAllocationSyncAll(rs_allocation alloc,
408                         rs_allocation_usage_type source);
409
410#endif //defined(RS_VERSION) && (RS_VERSION >= 14)
411
412/**
413 * Low performance utility function for drawing a simple rectangle.  Not
414 * intended for drawing large quantities of geometry.
415 *
416 * @param x1
417 * @param y1
418 * @param x2
419 * @param y2
420 * @param z
421 */
422extern void __attribute__((overloadable))
423    rsgDrawRect(float x1, float y1, float x2, float y2, float z);
424
425/**
426 * Low performance utility function for drawing a simple quad.  Not intended for
427 * drawing large quantities of geometry.
428 *
429 * @param x1
430 * @param y1
431 * @param z1
432 * @param x2
433 * @param y2
434 * @param z2
435 * @param x3
436 * @param y3
437 * @param z3
438 * @param x4
439 * @param y4
440 * @param z4
441 */
442extern void __attribute__((overloadable))
443    rsgDrawQuad(float x1, float y1, float z1,
444                float x2, float y2, float z2,
445                float x3, float y3, float z3,
446                float x4, float y4, float z4);
447
448
449/**
450 * Low performance utility function for drawing a textured quad.  Not intended
451 * for drawing large quantities of geometry.
452 *
453 * @param x1
454 * @param y1
455 * @param z1
456 * @param u1
457 * @param v1
458 * @param x2
459 * @param y2
460 * @param z2
461 * @param u2
462 * @param v2
463 * @param x3
464 * @param y3
465 * @param z3
466 * @param u3
467 * @param v3
468 * @param x4
469 * @param y4
470 * @param z4
471 * @param u4
472 * @param v4
473 */
474extern void __attribute__((overloadable))
475    rsgDrawQuadTexCoords(float x1, float y1, float z1, float u1, float v1,
476                         float x2, float y2, float z2, float u2, float v2,
477                         float x3, float y3, float z3, float u3, float v3,
478                         float x4, float y4, float z4, float u4, float v4);
479
480
481/**
482 * Low performance function for drawing rectangles in screenspace.  This
483 * function uses the default passthough ProgramVertex.  Any bound ProgramVertex
484 * is ignored.  This function has considerable overhead and should not be used
485 * for drawing in shipping applications.
486 *
487 * @param x
488 * @param y
489 * @param z
490 * @param w
491 * @param h
492 */
493extern void __attribute__((overloadable))
494    rsgDrawSpriteScreenspace(float x, float y, float z, float w, float h);
495
496extern void __attribute__((overloadable))
497    rsgDrawPath(rs_path p);
498
499/**
500 * Draw a mesh using the current context state.  The whole mesh is
501 * rendered.
502 *
503 * @param ism
504 */
505extern void __attribute__((overloadable))
506    rsgDrawMesh(rs_mesh ism);
507/**
508 * Draw part of a mesh using the current context state.
509 * @param ism mesh object to render
510 * @param primitiveIndex for meshes that contain multiple primitive groups
511 *        this parameter specifies the index of the group to draw.
512 */
513extern void __attribute__((overloadable))
514    rsgDrawMesh(rs_mesh ism, uint primitiveIndex);
515/**
516 * Draw specified index range of part of a mesh using the current context state.
517 * @param ism mesh object to render
518 * @param primitiveIndex for meshes that contain multiple primitive groups
519 *        this parameter specifies the index of the group to draw.
520 * @param start starting index in the range
521 * @param len number of indices to draw
522 */
523extern void __attribute__((overloadable))
524    rsgDrawMesh(rs_mesh ism, uint primitiveIndex, uint start, uint len);
525
526/**
527 * Clears the rendering surface to the specified color.
528 *
529 * @param r
530 * @param g
531 * @param b
532 * @param a
533 */
534extern void __attribute__((overloadable))
535    rsgClearColor(float r, float g, float b, float a);
536
537/**
538 * Clears the depth suface to the specified value.
539 */
540extern void __attribute__((overloadable))
541    rsgClearDepth(float value);
542/**
543 * Draws text given a string and location
544 */
545extern void __attribute__((overloadable))
546    rsgDrawText(const char *, int x, int y);
547/**
548 * \overload
549 */
550extern void __attribute__((overloadable))
551    rsgDrawText(rs_allocation, int x, int y);
552/**
553 * Binds the font object to be used for all subsequent font rendering calls
554 * @param font object to bind
555 */
556extern void __attribute__((overloadable))
557    rsgBindFont(rs_font font);
558/**
559 * Sets the font color for all subsequent rendering calls
560 * @param r red component
561 * @param g green component
562 * @param b blue component
563 * @param a alpha component
564 */
565extern void __attribute__((overloadable))
566    rsgFontColor(float r, float g, float b, float a);
567/**
568 * Returns the bounding box of the text relative to (0, 0)
569 * Any of left, right, top, bottom could be NULL
570 */
571extern void __attribute__((overloadable))
572    rsgMeasureText(const char *, int *left, int *right, int *top, int *bottom);
573/**
574 * \overload
575 */
576extern void __attribute__((overloadable))
577    rsgMeasureText(rs_allocation, int *left, int *right, int *top, int *bottom);
578/**
579 * Computes an axis aligned bounding box of a mesh object
580 */
581extern void __attribute__((overloadable))
582    rsgMeshComputeBoundingBox(rs_mesh mesh, float *minX, float *minY, float *minZ,
583                                                float *maxX, float *maxY, float *maxZ);
584/**
585 * \overload
586 */
587__inline__ static void __attribute__((overloadable, always_inline))
588rsgMeshComputeBoundingBox(rs_mesh mesh, float3 *bBoxMin, float3 *bBoxMax) {
589    float x1, y1, z1, x2, y2, z2;
590    rsgMeshComputeBoundingBox(mesh, &x1, &y1, &z1, &x2, &y2, &z2);
591    bBoxMin->x = x1;
592    bBoxMin->y = y1;
593    bBoxMin->z = z1;
594    bBoxMax->x = x2;
595    bBoxMax->y = y2;
596    bBoxMax->z = z2;
597}
598
599#endif
600
601