本表来自网络,我对说明做了些修改。
Name Syntax Description abs abs(x) 返回x的绝对值。对x的每个元素都会独立计算一次。Absolute value (per component). acos acos(x) 返回x的反余弦值。对x的每个元素都会独立计算一次。Returns the arccosine of each component of x. all all(x) 检测x的所有元数的值是否为0.Test if all components of x are nonzero. any any(x) 检测x是否有某个元数的值为0.Test if any component of x is nonzero. asfloat asfloat(x) 将x转换为float类型。Convert the input type to a float. asin asin(x) 返回x的反正弦值。对x的每个元素都会独立计算一次。 asint asint(x) 将x转换为int类型。Convert the input type to an integer. asuint asuint(x) 将x转换为uint类型。 atan atan(x) 返回x的反正切值。 atan2 atan2(y, x) 返回y、x的反正切值。 ceil ceil(x) 返回大于或等于x的最小整数。 clamp clamp(x, min, max) 将x截取在[min, max]范围内。 clip clip(x) 如果x中存在值小于0的参数,则丢弃当前像素。 cos cos(x) 返回x的余弦值。 cosh cosh(x) 返回x的双曲余弦值。 cross cross(x, y) 返回x、y的叉积。 D3DCOLORtoUBYTE4 D3DCOLORtoUBYTE4(x) 混合和缩放4D向量x用于补偿一些对UBYTE4支持的硬件。Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware. ddx ddx(x) 返回关于屏幕坐标x轴的偏导数。 ddy ddy(x) 返回关于屏幕坐标y轴的偏导数。 degrees degrees(x) 将x(弧度)转换到角度。 determinant determinant(m) 返回的正方形矩阵m的行列式。 distance distance(x, y) 返回x、y之间的距离。 dot dot(x, y) 返回x、y的点积。 exp exp(x) 返回以e为底数,x为指数的指数函数值。 exp2 exp2(x) 返回以2为底数,x为指数的指数函数值。对x的每个字段都会计算一次。 faceforward faceforward(n, i, ng) 检测多边形是否位于正面。-n * sign(•(i, ng))。 floor floor(x) 返回小于等于x的最大整数。 fmod fmod(x, y) 返回x/y的浮点余数。 frac frac(x) 返回x的小数部分。 frexp frexp(x, exp) 返回x的尾数和指数。 fwidth fwidth(x) 返回 abs(ddx(x)) + abs(ddy(x)), GetRenderTargetSampleCount GetRenderTargetSampleCount() 返回渲染目标采样器的个数。Returns the number of render-target samples. GetRenderTargetSamplePosition GetRenderTargetSamplePosition(x) 返回关于给定采样器的一个采样点(x,y)。Returns a sample position (x,y) for a given sample index. isfinite isfinite(x) 如果x为有限值则返回true,否则返回false。 isinf isinf(x) 如果x为无限值则返回true,否则返回false。 isnan isnan(x) 如果x为NAN或QNAN则返回true,否则返回false。 ldexp ldexp(x, exp) frexp的逆运算,返回 x * 2 ^ exp。 length length(v) 返回v向量的长度。 lerp lerp(x, y, s) 对x、y进行插值计算。Returns x + s(y - x)。 lit lit(n • l, n • h, m) 返回光照向量(环境光,漫反射光,镜面高光,1)。 log log(x) 返回以e为底的对数。 log10 log10(x) 返回以10为底的对数。 log2 log2(x) 返回以2为底的对数。 max max(x, y) 返回x、y中较大值。 min min(x, y) 返回x、y中较小值。 modf modf(x, out ip) 把x分割为整数和小数部分。 mul mul(x, y) 返回x、y矩阵相乘的积。 noise noise(x) Generates a random value using the Perlin-noise algorithm. normalize normalize(x) 返回单位化向量,定义为 x / length(x)。 pow pow(x, y) 返回x^y。 radians radians(x) 将x(角度)转换到弧度。 reflect reflect(i, n) 返回入射光线i对表面法线n的反射光线。 refract refract(i, n, R) 返回在入射光线i,表面法线n,折射率为R下的折射光线。 round round(x) 返回最接近x的整数。 rsqrt rsqrt(x) 返回x平方根的倒数。 1 / sqrt(x) 。 saturate saturate(x) 把x截取在[0, 1]之间。 sign sign(x) 返回x的符号。 sin sin(x) 返回x的正弦值。 sincos sincos(x, out s, out c) 返回x的正弦值和余弦值。 sinh sinh(x) 返回x的双曲正弦值。 smoothstep smoothstep(min, max, x) 如果x的范围是[min, max],则返回一个介于0和1之间的Hermite插值。 sqrt sqrt(x) 返回x的平方根,对x的每个字段都会计算一次。 step step(a, x) 返回 (x >= a) ? 1 : 0 。 tan tan(x) 返回x的正切值。 tanh tanh(x) 返回x的双曲正切值。 tex1D tex1D(s, t) 返回纹理s在t位置的颜色。1D texture lookup. tex1Dbias tex1Dbias(s, t) 使用bias返回纹理s在t位置的颜色。1D texture lookup with bias. tex1Dgrad tex1Dgrad(s, t, ddx, ddy) 1D texture lookup with a gradient. tex1Dlod tex1Dlod(s, t) 使用LOD返回纹理s在t位置的颜色。1D texture lookup with LOD. tex1Dproj tex1Dproj(s, t) 使用透视分离返回纹理s在t位置的颜色。1D texture lookup with projective divide. tex2D tex2D(s, t) 返回纹理s在t位置的颜色。 tex2Dbias tex2Dbias(s, t) 2D texture lookup with bias. tex2Dgrad tex2Dgrad(s, t, ddx, ddy) 2D texture lookup with a gradient. tex2Dlod tex2Dlod(s, t) 2D texture lookup with LOD. tex2Dproj tex2Dproj(s, t) 2D texture lookup with projective divide. tex3D tex3D(s, t) 3D texture lookup. tex3Dbias tex3Dbias(s, t) 3D texture lookup with bias. tex3Dgrad tex3Dgrad(s, t, ddx, ddy) 3D texture lookup with a gradient. tex3Dlod tex3Dlod(s, t) 3D texture lookup with LOD. tex3Dproj tex3Dproj(s, t) 3D texture lookup with projective divide. texCUBE texCUBE(s, t) Cube texture lookup. texCUBEbias texCUBEbias(s, t) Cube texture lookup with bias. texCUBEgrad texCUBEgrad(s, t, ddx, ddy) Cube texture lookup with a gradient. texCUBElod tex3Dlod(s, t) Cube texture lookup with LOD. texCUBEproj texCUBEproj(s, t) Cube texture lookup with projective divide. transpose transpose(m) 返回m的转置矩阵。 trunc trunc(x) 将x的所有元素从浮点值截断到整数值。
到了2016年我们依旧还在各种着色语言中徘徊着,目前一线的着色语言有支持Direct3D的HLSL和支持OpenGL的GLSL以及作为“默认”前端语言为Vulkan服务的SPIR-V。SPIR-V这一中间层语言也许最终成为这一切的首选,但这还需要一段时间。所以现在,当你需要将HLSL为GLSL或将HLSL转换GLSL时 ,你仍需要将精力放在两者的Api上。
我今天不会深入讲到到交叉编译器——这会是一个很大的话题——而是更多关注这两种语言相似之处。你是否遇到过,SV_Position输入在GLSL中是什么?那么这篇文章正是给你的!
注意
这篇文章绝对是不够完整的。当你需求去了解GLSL和HLSL之间着色器区别的时候,它只能作为一个出发点。我省略了那些用于实例的相同函数。
Direct3D有很多系统参数,而相应的,GLSL也有內建的输入参数的概念。他们的对应关系如下:
HLSL
GLSL
SV_ClipDistance
gl_ClipDistance
SV_CullDistance
当存在ARB_cull_distance情况下gl_CullDistance
SV_Coverage
gl_SampleMaskIn & gl_SampleMask
SV_Depth
gl_FragDepth
SV_DepthGreaterEqual
layout (depth_greater) out float gl_FragDepth;
SV_DepthLessEqual
layout (depth_less) out float gl_FragDepth;
SV_DispatchThreadID
gl_GlobalInvocationID
SV_DomainLocation
gl_TessCord
SV_GroupID
gl_WorkGroupID
SV_GroupIndex
N/A
SV_GroupThreadID
gl_LocalInvocationID
SV_GSInstanceID
gl_InvocationID
SV_InsideTessFactor
gl_TessLevelInner
SV_InstanceID
gl_InstanceID & gl_InstanceIndex (后面的 Vulkan 会有不同的语义)
SV_IsFrontFace
gl_FrontFacing
SV_OutputControlPointID
gl_InvocationID
N / A
gl_PatchVerticesIn
SV_Position
在顶点着色器中gl_Position
在片元着色器中gl_FragCoord
SV_PrimitiveID
gl_PrimitiveID
SV_RenderTargetArrayIndex
gl_Layer
SV_SampleIndex
gl_SampleID
通过EvaluateAttributeAtSample可以达到等价
gl_SamplePosition
SV_StencilRef
当存在ARB_cull_distance情况下gl_FragStencilRef
SV_Target
layout(location=N) out your_var_name
SV_TessFactor
gl_TessLevelOuter
SV_VertexID
gl_VertexID & gl_VertexIndex (后面的Vulkan会有不同的语义)
SV_ViewportArrayIndex
gl_ViewportIndex
此表来源于OpenGL维基百科,HLSL语义文档以及GL_KHR_vulkan_glsl扩展规范。
原子操作的对应关系非常简单。将Interlocked换成atomic。因此InterlockedAdd则替换成atomicAdd,以此类推。唯一的区别就是InterlockedCompareExchange要换成atomicCompSwap。
HLSL中的groupshared 内存就是GLSL中的shared 内存。仅此而已。
HLSL
GLSL
GroupMemoryBarrierWithGroupSync
groupMemoryBarrier
和 barrier
GroupMemoryBarrier
groupMemoryBarrier
DeviceMemoryBarrierWithGroupSync
memoryBarrier, memoryBarrierImage, memoryBarrierImage和barrier
AllMemoryBarrierWithGroupSync
上面所有的内存栅栏 和 barrier
AllMemoryBarrier
上面所有的内存栅栏
N/A
memoryBarrierShared
在没有Vulkan之前,贴图是整体绑定的,不可能部分访问。幸运的是, Vulkan使用和HLSL类似的语义,使得这部分可以有所不同。这个主要区别在于,HLSL中访问方法是“纹理对象”的一部分,而在GLSL,他们使用的是自由函数。在HLSL中,您要用一个Sampler采样器去采样一张Texture纹理贴图如下:
Texture.Sample (Sampler, coordinate)
在GLSL中,你需要指定纹理的类型和采样器的类型,除此之外,基本是一样的:
texture (sampler2D(Texture, Sampler), coordinate)
HLSL
GLSL
CalculateLevelOfDetail & CalculateLevelOfDetailUnclamped
textureQueryLod
Load
texelFetch 和 texelFetchOffset
GetDimensions
textureSize, textureQueryLevels 和textureSamples
Gather
textureGather, textureGatherOffset, textureGatherOffsets
Sample, SampleBias
texture, textureOffset
SampleCmp
samplerShadow
SampleGrad
textureGrad, textureGradOffset
SampleLevel
textureLod, textureLodOffset
N/A
textureProj
GLSL和HLSL对默认矩阵阐释有所不同。GLSL使用列优先右乘矩阵(也就是,你用的是 M * v),HLSL使用行优先左乘矩阵(v * M)然而你通常可以忽略这些-你可以重载这个命令,使之可以在左右两边都能进行乘法 –这将会改变矩阵m中m[0]的含义。在HLSL中,将返回第一行,而在GLSL中,则会返回第一列。,当你用“本来的”的命令初始化成员的时候,这同样也适用于构造函数。
HLSL
GLSL
atan2(y,x)
atan 使用参数交换
ddx
dFdx
ddx_coarse
dFdxCoarse
ddx_fine
dFdxFine
ddy
dFdy
ddy_coarse
dFdyCoarse
ddy_fine
dFdyFine
EvaluateAttributeAtCentroid
interpolateAtCentroid
EvaluateAttributeAtSample
interpolateAtSample
EvaluateAttributeSnapped
interpolateAtOffset
frac
fract
lerp
mix
mad
fma
saturate
clamp(x, 0.0, 1.0)
如果我还有什么疏漏的?请在下面评论,我会更新这篇文章!