ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Shader_GLSL、HLSL API异同

2021-04-29 20:30:02  阅读:374  来源: 互联网

标签:GLSL 返回 HLSL Shader texture API lookup gl


工作中,常常使用到GLSL或HLSL,对于其各自内置函数、各自独有函数、通用函数经常会出现混淆,现结合资料总结如下,方便后续查找。
本文主要参照khronos与微软官方文档:GLSLHLSL

注:SPIR-V中间层着色器语言可以通过Vulkan自带着色器转换工具进行处理(GLSL、HLSL to SPV),故此处不再赘述。

一、不同之处

1.1 系统参数与內建参数

D3D有很多系统参数,而相应的,GLSL也有內建的输入参数的概念。他们的对应关系如下:

HLSLGLSL
SV_ClipDistancegl_ClipDistance
SV_CullDistance当存在ARB_cull_distance情况下gl_CullDistance
SV_Coveragegl_SampleMaskIn & gl_SampleMask
SV_Depthgl_FragDepth
SV_DepthGreaterEquallayout (depth_greater) out float gl_FragDepth;
SV_DepthLessEquallayout (depth_less) out float gl_FragDepth;
SV_DispatchThreadIDgl_GlobalInvocationID
SV_DomainLocationgl_TessCord
SV_GroupIDgl_WorkGroupID
SV_GroupIndexN/A
SV_GroupThreadIDgl_LocalInvocationID
SV_GSInstanceIDgl_InvocationID
SV_InsideTessFactorgl_TessLevelInner
SV_InstanceIDgl_InstanceID & gl_InstanceIndex (后面的 Vulkan 会有不同的语义)
SV_IsFrontFacegl_FrontFacing
SV_OutputControlPointIDgl_InvocationID
N / Agl_PatchVerticesIn
SV_PositionVS:gl_Position;PS:gl_FragCoord
SV_PrimitiveIDgl_PrimitiveID
SV_RenderTargetArrayIndexgl_Layer
SV_SampleIndexgl_SampleID
通过EvaluateAttributeAtSample可以达到等价gl_SamplePosition
SV_StencilRef当存在ARB_cull_distance情况下gl_FragStencilRef
SV_Targetlayout(location=N) out your_var_name
SV_TessFactorgl_TessLevelOuter
SV_VertexIDgl_VertexID & gl_VertexIndex (后面的Vulkan会有不同的语义)
SV_ViewportArrayIndexgl_ViewportIndex

1.2 原子操作

原子操作的对应关系非常简单。将Interlocked换成atomic。因此InterlockedAdd则替换成atomicAdd,以此类推。唯一的区别就是InterlockedCompareExchange要换成atomicCompSwap。

1.3 共享/本地内存

HLSL中的groupshared 内存就是GLSL中的shared 内存。仅此而已。

1.4 内存屏障

HLSLGLSL
GroupMemoryBarrierWithGroupSyncgroupMemoryBarrier 和 barrier
GroupMemoryBarriergroupMemoryBarrier
DeviceMemoryBarrierWithGroupSyncmemoryBarrier,memoryBarrierImage, memoryBarrierImage和barrier
AllMemoryBarrierWithGroupSync上面所有的内存栅栏 和 barrier
AllMemoryBarrier上面所有的内存栅栏
N/AmemoryBarrierShared

1.5 纹理访问

在没有Vulkan之前,贴图是整体绑定的,不可能部分访问。幸运的是, Vulkan使用和HLSL类似的语义,使得这部分可以有所不同。这个主要区别在于,HLSL中访问方法是“纹理对象”的一部分,而在GLSL,他们使用的是自由函数。在HLSL中,您要用一个Sampler采样器去采样一张Texture纹理贴图如下:

Texture.Sample (Sampler, coordinate)

在GLSL中,你需要指定纹理的类型和采样器的类型,除此之外,基本是一样的:

texture (sampler2D(Texture, Sampler), coordinate)

具体采样相关函数:

HLSLGLSL
CalculateLevelOfDetail & CalculateLevelOfDetailUnclampedtextureQueryLod
LoadtexelFetch 和 texelFetchOffset
GetDimensionstextureSize, textureQueryLevels 和textureSamples
GathertextureGather, textureGatherOffset, textureGatherOffsets
Sample, SampleBiastexture, textureOffset
SampleCmpsamplerShadow
SampleGradtextureGrad, textureGradOffset
SampleLeveltextureLod, textureLodOffset
N/AtextureProj

1.6 矩阵运算

GLSL和HLSL对默认矩阵阐释有所不同。GLSL使用列优先右乘矩阵(也就是,你用的是 M * V),HLSL使用行优先左乘矩阵(V* M)然而你通常可以忽略这些-你可以重载这个命令,使之可以在左右两边都能进行乘法 –这将会改变矩阵m中m[0]的含义。在HLSL中,将返回第一行,而在GLSL中,则会返回第一列。,当你用“本来的”的命令初始化成员的时候,这同样也适用于构造函数。

1.7 其他异函数

HLSLGLSL
atan2(y,x)atan 使用参数交换
ddxdFdx
ddx_coarsedFdxCoarse
ddx_finedFdxFine
ddydFdy
ddy_coarsedFdyCoarse
ddy_finedFdyFine
EvaluateAttributeAtCentroidinterpolateAtCentroid
EvaluateAttributeAtSampleinterpolateAtSample
EvaluateAttributeSnappedinterpolateAtOffset
fracfract
lerpmix
madfma
saturateclamp(x, 0.0, 1.0)

二、相同之处

2.1 公有函数

函数名用法详述
absabs(x)返回x的绝对值。对x的每个元素都会独立计算一次。Absolute value (per component).
acosacos(x)返回x的反余弦值。对x的每个元素都会独立计算一次。Returns the arccosine of each component of x.
allall(x)检测x的所有元数的值是否为0.Test if all components of x are nonzero.
anyany(x)检测x是否有某个元数的值为0.Test if any component of x is nonzero.
asfloatasfloat(x)将x转换为float类型。Convert the input type to a float.
asinasin(x)返回x的反正弦值。对x的每个元素都会独立计算一次。
asintasint(x)将x转换为int类型。Convert the input type to an integer.
asuintasuint(x)将x转换为uint类型。
atanatan(x)返回x的反正切值。
atan2atan2(y, x)返回y、x的反正切值。
ceilceil(x)返回大于或等于x的最小整数。
clampclamp(x, min, max)将x截取在[min, max]范围内。
clipclip(x)如果x中存在值小于0的参数,则丢弃当前像素。
coscos(x)返回x的余弦值。
coshcosh(x)返回x的双曲余弦值。
crosscross(x, y)返回x、y的叉积。
D3DCOLORtoUBYTE4D3DCOLORtoUBYTE4(x)混合和缩放4D向量x用于补偿一些对UBYTE4支持的硬件。Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware.
ddxddx(x)返回关于屏幕坐标x轴的偏导数。
ddyddy(x)返回关于屏幕坐标y轴的偏导数。
degreesdegrees(x)将x(弧度)转换到角度。
determinantdeterminant(m)返回的正方形矩阵m的行列式。
distancedistance(x, y)返回x、y之间的距离。
dotdot(x, y)返回x、y的点积。
expexp(x)返回以e为底数,x为指数的指数函数值。
exp2exp2(x)返回以2为底数,x为指数的指数函数值。对x的每个字段都会计算一次。
faceforwardfaceforward(n, i, ng)检测多边形是否位于正面。-n * sign(•(i, ng))。
floorfloor(x)返回小于等于x的最大整数。
fmodfmod(x, y)返回x/y的浮点余数。
fracfrac(x)返回x的小数部分。
frexpfrexp(x, exp)返回x的尾数和指数。
fwidthfwidth(x)返回 abs(ddx(x)) + abs(ddy(x)),
GetRenderTargetSampleCountGetRenderTargetSampleCount()返回渲染目标采样器的个数。Returns the number of render-target samples.
GetRenderTargetSamplePositionGetRenderTargetSamplePosition(x)返回关于给定采样器的一个采样点(x,y)。Returns a sample position (x,y) for a given sample index.
isfiniteisfinite(x)如果x为有限值则返回true,否则返回false。
isinfisinf(x)如果x为无限值则返回true,否则返回false。
isnanisnan(x)如果x为NAN或QNAN则返回true,否则返回false。
ldexpldexp(x, exp)frexp的逆运算,返回 x * 2 ^ exp。
lengthlength(v)返回v向量的长度。
lerplerp(x, y, s)对x、y进行插值计算。Returns x + s(y - x)。
litlit(n • l, n • h, m)返回光照向量(环境光,漫反射光,镜面高光,1)。
loglog(x)返回以e为底的对数。
log10log10(x)返回以10为底的对数。
log2log2(x)返回以2为底的对数。
maxmax(x, y)返回x、y中较大值。
minmin(x, y)返回x、y中较小值。
modfmodf(x, out ip)把x分割为整数和小数部分。
mulmul(x, y)返回x、y矩阵相乘的积。
noisenoise(x)Generates a random value using the Perlin-noise algorithm.
normalizenormalize(x)返回单位化向量,定义为 x / length(x)。
powpow(x, y)返回x^y。
radiansradians(x)将x(角度)转换到弧度。
reflectreflect(i, n)返回入射光线i对表面法线n的反射光线。
refractrefract(i, n, R)返回在入射光线i,表面法线n,折射率为R下的折射光线。
roundround(x)返回最接近x的整数。
rsqrtrsqrt(x)返回x平方根的倒数。 1 / sqrt(x) 。
saturatesaturate(x)把x截取在[0, 1]之间。
signsign(x)返回x的符号。
sinsin(x)返回x的正弦值。
sincossincos(x, out s, out c)返回x的正弦值和余弦值。
sinhsinh(x)返回x的双曲正弦值。
smoothstepsmoothstep(min, max, x)如果x的范围是[min, max],则返回一个介于0和1之间的Hermite插值。
sqrtsqrt(x)返回x的平方根,对x的每个字段都会计算一次。
stepstep(a, x)返回 (x >= a) ? 1 : 0 。
tantan(x)返回x的正切值。
tanhtanh(x)返回x的双曲正切值。
tex1Dtex1D(s, t)返回纹理s在t位置的颜色。1D texture lookup.
tex1Dbiastex1Dbias(s, t)使用bias返回纹理s在t位置的颜色。1D texture lookup with bias.
tex1Dgradtex1Dgrad(s, t, ddx, ddy)1D texture lookup with a gradient.
tex1Dlodtex1Dlod(s, t)使用LOD返回纹理s在t位置的颜色。1D texture lookup with LOD.
tex1Dprojtex1Dproj(s, t)使用透视分离返回纹理s在t位置的颜色。1D texture lookup with projective divide.
tex2Dtex2D(s, t)返回纹理s在t位置的颜色。
tex2Dbiastex2Dbias(s, t)2D texture lookup with bias.
tex2Dgradtex2Dgrad(s, t, ddx, ddy)2D texture lookup with a gradient.
tex2Dlodtex2Dlod(s, t)2D texture lookup with LOD.
tex2Dprojtex2Dproj(s, t)2D texture lookup with projective divide.
tex3Dtex3D(s, t)3D texture lookup.
tex3Dbiastex3Dbias(s, t)3D texture lookup with bias.
tex3Dgradtex3Dgrad(s, t, ddx, ddy)3D texture lookup with a gradient.
tex3Dlodtex3Dlod(s, t)3D texture lookup with LOD.
tex3Dprojtex3Dproj(s, t)3D texture lookup with projective divide.
texCUBEtexCUBE(s, t)Cube texture lookup.
texCUBEbiastexCUBEbias(s, t)Cube texture lookup with bias.
texCUBEgradtexCUBEgrad(s, t, ddx, ddy)Cube texture lookup with a gradient.
texCUBElodtex3Dlod(s, t)Cube texture lookup with LOD.
texCUBEprojtexCUBEproj(s, t)Cube texture lookup with projective divide.
transposetranspose(m)返回m的转置矩阵。
trunctrunc(x)将x的所有元素从浮点值截断到整数值。

未完,后续用到再补充…

标签:GLSL,返回,HLSL,Shader,texture,API,lookup,gl
来源: https://blog.csdn.net/qq_35312463/article/details/116140040

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有