您的位置:首页 > 其它

计算:RenderScript Runtime API参考:简介

2017-05-12 09:31 267 查看
一、概论

RenderScript是一个高性能的运行时,可以在本机级别提供计算操作。 RenderScript代码在运行时在设备上编译,以允许平台独立性。

本参考文档介绍了RenderScript运行时API,您可以使用它来编写C99中的RenderScript代码。 自动为你包含RenderScript计算头文件。

要使用RenderScript,您需要使用此处记录的RenderScript运行时API以及RenderScript的Android框架API。 有关Android框架API的文档,请参阅android.renderscript软件包参考。

有关如何使用RenderScript开发以及运行时和Android框架API如何交互的更多信息,请参阅RenderScript开发人员指南和RenderScript示例。

二、数值类型

1、标量

RenderScript支持以下标量数值类型:

RenderScript支持长度为2,3和4.的固定大小向量。使用通用类型名称后跟2,3或4.声明向量。 float4,int3,double2,ulong4。

8 bits16 bits32 bits64 bits
Integer:char, int8_tshort, int16_tint32_tlong, long long, int64_t
Unsigned integer:uchar, uint8_tushort, uint16_tuint, uint32_tulong, uint64_t
Floating point:halffloatdouble
2、矢量

RenderScript支持长度为2,3和4.的固定大小向量。使用通用类型名称后跟2,3或4.声明向量。 float4,int3,double2,ulong4。

要创建矢量文字,请使用矢量类型,后跟大括号之间的值,例如。 (float3){1.0f,2.0f,3.0f}。

可以使用不同的命名样式来访问向量的条目。

可以通过使用点跟随变量名称来访问单个条目:

1、字母x,y,z和w,

2、字母r,g,b和a,

3、字母s或S,后跟零索引。

例如,使用int4 myVar; 以下是等效的:

myVar.x == myVar.r == myVar.s0 == myVar.S0
myVar.y == myVar.g == myVar.s1 == myVar.S1
myVar.z == myVar.b == myVar.s2 == myVar.S2
myVar.w == myVar.a == myVar.s3 == myVar.S3


可以通过使用多个字母或索引的连接的标识符一次访问向量的多个条目。 结果向量的大小等于命名的条目数。

通过上面的例子,可以使用myVar.yz,myVar.gb,myVar.s12和myVar.S12访问中间的两个条目。

条目不必是连续的或按照递增的顺序。 条目甚至可以重复,只要我们不试图分配它。 您也不能混合命名样式。

以下是可以做什么或不能做的例子:

float4 v4;
float3 v3;
float2 v2;
v2 = v4.xx; // Valid
v3 = v4.zxw; // Valid
v3 = v4.bba; // Valid
v3 = v4.s032; // Valid
v3.s120 = v4.S233; // Valid
v4.yz = v3.rg; // Valid
v4.yzx = v3.rg; // Invalid: mismatched sizes
v4.yzz = v3; // Invalid: z appears twice in an assignment
v3 = v3.xas0; // Invalid: can't mix xyzw with rgba nor s0...
v3 = v4.s034; // Invalid: the digit can only be 0, 1, 2, or 3


3、矩阵和四元数

RenderScript支持大小为2x2,3x3和4x4的固定尺寸的浮点矩阵。 这些类型命名为rs_matrix2x2,rs_matrix3x3和rs_matrix4x4。 有关操作列表,请参阅矩阵函数。

四元数也被rs四元数支持。 有关操作列表,请参见四元数函数。

Types
char2Two 8 bit signed integers
char3Three 8 bit signed integers
char4Four 8 bit signed integers
double2Two 64 bit floats
double3Three 64 bit floats
double4Four 64 bit floats
float2Two 32 bit floats
float3Three 32 bit floats
float4Four 32 bit floats
half16 bit floating point value
half2Two 16 bit floats
half3Three 16 bit floats
half4Four 16 bit floats
int16_t16 bit signed integer
int2Two 32 bit signed integers
int3Three 32 bit signed integers
int32_t32 bit signed integer
int4Four 32 bit signed integers
int64_t64 bit signed integer
int8_t8 bit signed integer
long2Two 64 bit signed integers
long3Three 64 bit signed integers
long4Four 64 bit signed integers
rs_matrix2x22x2 matrix of 32 bit floats
rs_matrix3x33x3 matrix of 32 bit floats
rs_matrix4x44x4 matrix of 32 bit floats
rs_quaternionQuaternion
short2Two 16 bit signed integers
short3Three 16 bit signed integers
short4Four 16 bit signed integers
size_tUnsigned size type
ssize_tSigned size type
uchar8 bit unsigned integer
uchar2Two 8 bit unsigned integers
uchar3Three 8 bit unsigned integers
uchar4Four 8 bit unsigned integers
uint32 bit unsigned integer
uint16_t16 bit unsigned integer
uint2Two 32 bit unsigned integers
uint3Three 32 bit unsigned integers
uint32_t32 bit unsigned integer
uint4Four 32 bit unsigned integers
uint64_t64 bit unsigned integer
uint8_t8 bit unsigned integer
ulong64 bit unsigned integer
ulong2Two 64 bit unsigned integers
ulong3Three 64 bit unsigned integers
ulong4Four 64 bit unsigned integers
ushort16 bit unsigned integer
ushort2Two 16 bit unsigned integers
ushort3Three 16 bit unsigned integers
ushort4Four 16 bit unsigned integers
三、对象类型

以下类型用于操作RenderScript对象,如分配,采样器,元素和脚本。 大多数这些对象是使用Java RenderScript API创建的。

Types
rs_allocationHandle to an allocation
rs_allocation_cubemap_faceEnum for selecting cube map faces
rs_allocation_usage_typeBitfield to specify how an allocation is used
rs_data_kindElement data kind
rs_data_typeElement basic data type
rs_elementHandle to an element
rs_samplerHandle to a Sampler
rs_sampler_valueSampler wrap T value
rs_scriptHandle to a Script
rs_typeHandle to a Type
四、转换函数

以下功能将数字矢量类型转换为另一种,或从一种颜色表示转换为另一种。

Functions
convertConvert numerical vectors
rsPackColorTo8888Create a uchar4 RGBA from floats
rsUnpackColor8888Create a float4 RGBA from uchar4
rsYuvToRGBAConvert a YUV value to RGBA
五、数学常数和函数

下面的数学函数可以应用于标量和向量。 当应用于向量时,返回的值是应用于输入的每个条目的函数的向量。

例如:

float3 a, b;
// The following call sets
// a.x to sin(b.x),
// a.y to sin(b.y), and
// a.z to sin(b.z).
a = sin(b);


参见vector()函数,如distance()和length(),将输入解释为n维空间中的单个向量。

32位浮点数学运算的精度受编译指示rs_fp_relaxed和rs_fp_full的影响。 在rs_fp_relaxed下,可能会将低于正常值的值刷新为零,并可以向零舍入。 相比之下,rs_fp_full需要对正常值进行正确处理,即小于1.17549435e-38f。 rs_fp_rull也需要一轮到最接近的连线。

通过使用常用数学函数的变体可以实现不同的精度/速度权衡。 名称以

1、native_:可能具有较低精度的定制硬件实现。 另外,亚正态值可以被刷新为零,可以使用向零舍入,并且可以不正确地处理NaN和无穷大输入。

2、half_:可以使用16位浮点进行内部计算。 另外,可以将次正规值刷新为零,并且可以使用向零舍入。

常量
M_1_PI1 / pi, as a 32 bit float
M_2_PI2 / pi, as a 32 bit float
M_2_SQRTPI2 / sqrt(pi), as a 32 bit float
M_Ee, as a 32 bit float
M_LN10log_e(10), as a 32 bit float
M_LN2log_e(2), as a 32 bit float
M_LOG10Elog_10(e), as a 32 bit float
M_LOG2Elog_2(e), as a 32 bit float
M_PIpi, as a 32 bit float
M_PI_2pi / 2, as a 32 bit float
M_PI_4pi / 4, as a 32 bit float
M_SQRT1_21 / sqrt(2), as a 32 bit float
M_SQRT2sqrt(2), as a 32 bit float
函数
absAbsolute value of an integer
acosInverse cosine
acoshInverse hyperbolic cosine
acospiInverse cosine divided by pi
asinInverse sine
asinhInverse hyperbolic sine
asinpiInverse sine divided by pi
atanInverse tangent
atan2Inverse tangent of a ratio
atan2piInverse tangent of a ratio, divided by pi
atanhInverse hyperbolic tangent
atanpiInverse tangent divided by pi
cbrtCube root
ceilSmallest integer not less than a value
clampRestrain a value to a range
clzNumber of leading 0 bits
copysignCopies the sign of a number to another
cosCosine
coshHypebolic cosine
cospiCosine of a number multiplied by pi
degreesConverts radians into degrees
erfMathematical error function
erfcMathematical complementary error function
expe raised to a number
exp1010 raised to a number
exp22 raised to a number
expm1e raised to a number minus one
fabsAbsolute value of a float
fdimPositive difference between two values
floorSmallest integer not greater than a value
fmaMultiply and add
fmaxMaximum of two floats
fminMinimum of two floats
fmodModulo
fractPositive fractional part
frexpBinary mantissa and exponent
half_recipReciprocal computed to 16 bit precision
half_rsqrtReciprocal of a square root computed to 16 bit precision
half_sqrtSquare root computed to 16 bit precision
hypotHypotenuse
ilogbBase two exponent
ldexpCreates a floating point from mantissa and exponent
lgammaNatural logarithm of the gamma function
logNatural logarithm
log10Base 10 logarithm
log1pNatural logarithm of a value plus 1
log2Base 2 logarithm
logbBase two exponent
madMultiply and add
maxMaximum
minMinimum
mixMixes two values
modfIntegral and fractional components
nanNot a Number
native_acosApproximate inverse cosine
native_acoshApproximate inverse hyperbolic cosine
native_acospiApproximate inverse cosine divided by pi
native_asinApproximate inverse sine
native_asinhApproximate inverse hyperbolic sine
native_asinpiApproximate inverse sine divided by pi
native_atanApproximate inverse tangent
native_atan2Approximate inverse tangent of a ratio
native_atan2piApproximate inverse tangent of a ratio, divided by pi
native_atanhApproximate inverse hyperbolic tangent
native_atanpiApproximate inverse tangent divided by pi
native_cbrtApproximate cube root
native_cosApproximate cosine
native_coshApproximate hypebolic cosine
native_cospiApproximate cosine of a number multiplied by pi
native_divideApproximate division
native_expApproximate e raised to a number
native_exp10Approximate 10 raised to a number
native_exp2Approximate 2 raised to a number
native_expm1近似e上升到数字减1
native_hypot近似斜边
native_log近似自然对数
native_log10近似基数10对数
native_log1p值的近似自然对数加1
native_log2近似基数2对数
native_powr近似积极的基数升至指数
native_recip近似倒数
native_rootn近似第n个根
native_rsqrt平方根的近似倒数
native_sin近似正弦
native_sincos大概正弦和余弦
native_sinh近似双曲正弦
native_sinpi一个数字的近似正弦乘以pi
native_sqrt近似平方根
native_tan近似切线
native_tanh近似双曲正切
native_tanpi一个数字的近似正切乘以pi
nextafter下一个浮点数
pow基数提升到指数
pown基数提升为整数指数
powr正的基数上升到指数
radians将度数转换为弧度
remainder剩余部门
remquo一个除法的余数和商数
rintRound to even
rootn第N根
round从零开始
rsRand伪随机数
rsqrt平方根的倒数
sign一个价值的标志
sin正弦
sincos正弦和余弦
sinh双曲正弦
sinpi正弦数乘以pi
sqrt平方根
step如果小于0,则为0,否则为0
tan切线
tanh双曲正切
tanpi一个数字的相切乘以pi
tgamma伽玛功能
trunc截断一个浮点
六、矢量数学函数

这些函数将输入参数解释为n维空间中向量的表示。

32位浮点数学运算的精度受编译指示rs_fp_relaxed和rs_fp_full的影响。 有关详细信息,请参见数学常数和函数。

通过使用常用数学函数的变体可以实现不同的精度/速度权衡。 名称以

1、native_:可能具有较低精度的定制硬件实现。 另外,亚正态值可以被刷新为零,可以使用向零舍入,并且可以不正确地处理NaN和无穷大输入。

2、fast_:可以使用16位浮点进行内部计算。 另外,可以将次正规值刷新为零,并且可以使用向零舍入。

Functions
cross两个向量的交叉乘积
distance两点之间的距离
dot两个向量的点积
fast_distance两点之间的近似距离
fast_length矢量的近似长度
fast_normalize近似归一化矢量
length一个向量的长度
native_distance两点之间的近似距离
native_length矢量的近似长度
native_normalize大概归一化一个向量
normalize归一化矢量
七、矩阵函数

这些函数可以让您处理2×2,3×3和4×4级的方阵。 它们对于图形转换特别有用,并且与OpenGL兼容。

我们对行和列使用基于零的索引。 例如。 (3,3)找到rs_matrix4x4的最后一个元素。

RenderScript使用列主要矩阵和基于列的向量。 通过后向量来完成向量的变换,例如 (matrix * vector),由rsMatrixMultiply()提供。

为了创建一次执行两个变换的变换矩阵,乘以两个源矩阵,第一个变换作为正确的参数。 例如。 创建一个转换矩阵,该矩阵应用s1后跟s2,调用rsMatrixLoadMultiply(&combined,&s2,&s1)。 这来自s2 *(s1 * v),它是(s2 * s1)* v。

我们有两种风格的函数来创建转换矩阵:rsMatrixLoadTransformation和rsMatrixTransformation。 前一种风格只是将变换矩阵存储在第一个参数中。 后者修改了一个预先存在的转换矩阵,以便首先发生新的变换。 例如。 如果在已经进行缩放的矩阵上调用rsMatrixTranslate(),则应用于向量的矩阵将首先进行转换,然后进行缩放。

函数
rsExtractFrustumPlanes计算平头锥体水平面
rsIsSphereInFrustum检查球体是否在平截头体水平面内
rsMatrixGet获得一个元素
rsMatrixInverse将矩阵反转到位
rsMatrixInverseTranspose反转并将矩阵转置到位
rsMatrixLoad加载或复制矩阵
rsMatrixLoadFrustum加载平截头体投影矩阵
rsMatrixLoadIdentity加载标识矩阵
rsMatrixLoadMultiply乘以两个矩阵
rsMatrixLoadOrtho加载正投影矩阵
rsMatrixLoadPerspective加载透视投影矩阵
rsMatrixLoadRotate加载旋转矩阵
rsMatrixLoadScale加载缩放矩阵
rsMatrixLoadTranslate加载一个翻译矩阵
rsMatrixMultiply通过矢量或另一个矩阵乘以一个矩阵
rsMatrixRotate对转换矩阵应用旋转
rsMatrixScale应用缩放到转换矩阵
rsMatrixSet设置一个元素
rsMatrixTranslate将转换应用于转换矩阵
rsMatrixTranspose转置矩阵位置
八、四元函数

以下函数操纵四元数。

函数
rsQuaternionAdd添加两个四元数
rsQuaternionConjugate共轭四元数
rsQuaternionDot两个四元数的点积
rsQuaternionGetMatrixUnit从四元数得到旋转矩阵
rsQuaternionLoadRotate创建一个旋转四元数
rsQuaternionLoadRotateUnit表示围绕任意单位向量的旋转的四元数
rsQuaternionMultiply将四元数乘以标量或其他四元数
rsQuaternionNormalize归一化四元数
rsQuaternionSet创建四元数
rsQuaternionSlerp两个四元数之间的球面线性插值
九、原子更新函数

要更新多个线程之间共享的值,请使用以下功能。 它们确保值被原子更新,即存储器读取,更新和存储器写入以正确的顺序完成。

这些功能比其非原子等价物要慢,因此只有在需要同步时才使用它们。

请注意,在RenderScript中,尽管您没有明确创建它们,您的代码可能会在单独的线程中运行。 RenderScript运行时将经常将多个线程中的一个内核的执行拆分。 更新全局变量应该用原子函数完成。 如果可能,修改您的算法以避免它们完全相同。

函数
rsAtomicAdd线程安全添加
rsAtomicAnd线程安全按位和
rsAtomicCas线程安全比较和设置
rsAtomicDec线程安全递减
rsAtomicInc线程安全增量
rsAtomicMax线程安全最大值
rsAtomicMin线程安全最小
rsAtomicOr线程安全按位或
rsAtomicSub线程安全减法
rsAtomicXor线程安全按位排序或
十、时间函数和类型

以下功能可用于说明当前时钟时间和当前系统正常运行时间。 不建议在内核中调用这些函数。

类型
rs_time_t自1970.1.1以来的秒数
rs_tm日期和时间结构
函数
rsGetDt自上次电话以来的经过时间
rsLocaltime转换到当地时间
rsTime自1970.1.1以来的秒数
rsUptimeMillis系统正常运行时间(以毫秒为单位)
rsUptimeNanos系统正常运行时间,以纳秒为单位
十一、分配数据访问功能

以下函数可用于获取和设置构成分配的单元格。

1、使用rs GetElementAt *和SetTlement函数访问各个单元格。

2、可以使用rsAllocationCopy *和rsAllocationV *函数复制多个单元格。

3、要通过采样器获取值,请使用rsSample。

rsGetElementAt和rsSetElement *函数有点不明确。 他们没有获取或设置元素,类似于数据类型; 他们得到或设置单元格。 将它们视为rsGetCellAt和rsSetCellAt。

函数
rsAllocationCopy1DRange在分配之间复制连续的单元格
rsAllocationCopy2DRange在分配之间复制单元格的矩形区域
rsAllocationVLoadX从标量分配中获取向量
rsAllocationVStoreX将向量存储到标量的分配中
rsGetElementAt从分配中返回一个单元格
rsGetElementAtYuv_uchar_U获取分配YUV的U分量
rsGetElementAtYuv_uchar_V获取分配YUV的V分量
rsGetElementAtYuv_uchar_Y获取Y分配的Y分量
rsSample从纹理分配中抽取一个值
rsSetElementAt设置一个分配单元格
十二、对象特征功能

以下功能可用于查询分配,元素或采样器对象的特性。 这些对象是从Java创建的。 您无法从脚本创建它们。

1、分配

分配是用于向RenderScript内核传递数据的主要方法。

它们是可用于存储位图,纹理,任意数据点等的单元格的结构化集合。

单元格的这个集合可能具有许多维度(X,Y,Z,Array0,Array1,Array2,Array3),面(用于立方体)和细节级别(用于mipmap)。

有关创建分配的详细信息,请参阅android.renderscript.Allocation。

2、元素

术语“元素”在RenderScript中有点模糊,作为分配单元格的类型信息和该类型的实例化。 例如:

1)、rs_element是类型规范的句柄,

2)、在诸如rsGetElementAt()的函数中,“元素”表示类型的实例化,即分配的单元格。

以下函数可以查询类型规格的特征。

元素可以指定C中所发现的简单数据类型,例如。 一个整数,float或boolean。 它还可以指定一个RenderScript对象的句柄。 有关基本类型的列表,请参阅rs_data_type。

元素可以指定基本类型的固定大小向量(大小为2,3或4)版本。 元素可以组合成复杂的元素,创建相当于C结构定义。

元素也可以有一种,这是用于解释像素数据的语义信息。 见rs_data_kind。

当创建公共元素的分配时,您可以简单地使用许多预定义元素之一,如F32_2。

要创建复杂的元素,请使用Element.Builder Java类。

3、取样

采样器对象定义如何将内存中的结构读取为分配。 参见android.renderscript.S。

函数
rsAllocationGetDimFacesPresence of more than one face
rsAllocationGetDimLODPresence of levels of detail(存在细节级别)
rsAllocationGetDimXX维度的尺寸
rsAllocationGetDimYY维度的尺寸
rsAllocationGetDimZZ维度的尺寸
rsAllocationGetElement获取描述分配单元格的对象
rsClearObject释放一个对象
rsElementGetBytesSize元素的大小
rsElementGetDataKind元素的种类
rsElementGetDataType元素的数据类型
rsElementGetSubElement复杂元素的子元素
rsElementGetSubElementArraySize复数元素的子元素的数组大小
rsElementGetSubElementCount子元素数量
rsElementGetSubElementName子元素的名称
rsElementGetSubElementNameLength子元素名称的长度
rsElementGetSubElementOffsetBytes实例子元素的偏移量
rsElementGetVectorSize元素的矢量大小
rsIsObject检查一个空的句柄
rsSamplerGetAnisotropy取样器的各向异性
rsSamplerGetMagnification取样器放大值
rsSamplerGetMinification取样器最小化值
rsSamplerGetWrapS采样器包裹S值
rsSamplerGetWrapT采样器包裹T值
十三、内核调用函数和类型

rsForEach()函数可用于调用脚本的根内核。

其他功能用于获取执行内核调用的特性,如维度和当前索引。 这些函数以rs_kernel_context为参数。

类型
rs_for_each_strategy_t建议单元格处理顺序
rs_kernel_context处理内核调用上下文
rs_script_call_t单元格迭代信息
函数
rsForEach调用脚本的根内核
rsGetArray0指定内核上下文的Array0维度中的索引
rsGetArray1指定内核上下文的Array1维度中的索引
rsGetArray2指定内核上下文的Array2维度中的索引
rsGetArray3指定内核上下文的Array3维度中的索引
rsGetDimArray0指定内核上下文的Array0维的大小
rsGetDimArray1指定内核上下文的Array1维的大小
rsGetDimArray2指定内核上下文的Array2维度的大小
rsGetDimArray3指定内核上下文的Array3维度的大小
rsGetDimHasFaces针对指定的内核上下文存在多个面
rsGetDimLod指定内核上下文的详细级别数
rsGetDimX指定内核上下文的X维的大小
rsGetDimY指定内核上下文的Y维的大小
rsGetDimZ指定内核上下文的Z维的大小
rsGetFace面向指定内核上下文的坐标
rsGetLod指定内核上下文的“细节级别”维度中的索引
十四、输入/输出功能

这些功能用于:

1、发送信息到Java客户端,和

2、发送处理后的分配或接收下一个分配进行处理。

函数
rsAllocationIoReceive从队列中接收新内容
rsAllocationIoSend发送新的内容到队列
rsSendToClient向客户端发送消息,不堵塞
rsSendToClientBlocking发送消息给客户端,阻塞
十五、调试功能

以下功能旨在在应用程序开发过程中使用。 它们不应用于运输应用程序。

函数
rsDebug记录消息和值
十六、图形功能和类型

RenderScript的图形子系统在API级别23被删除。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: