如何在Slang与HLSL中实现顶点着色器flat变量传递?
在Slang中实现GLSL的flat变量传递效果
你提供的GLSL顶点着色器代码如下:
layout(location = 2) out flat uint out_uniform_index; layout(location = 3) out vec2 out_pos_in_chamfer_box; layout(location = 4) out flat vec2 out_chamfer_box_size;
你提到GLSL中整数默认采用flat模式,浮点类型需显式标记该限定符,而Slang没有flat关键字,同时疑惑HLSL是否也没有该限定符,以下是对应的解决方案:
HLSL和Slang中,与GLSL flat语义完全等价的是nointerpolation关键字,它的作用就是禁用顶点输出到片元的插值计算,直接传递顶点的原始值(具体使用图元中哪个顶点的值遵循底层API的规则,和GLSL的flat行为一致)。
对应到你的代码,只需要把flat替换为nointerpolation即可实现相同效果:
layout(location = 2) out nointerpolation uint out_uniform_index; layout(location = 3) out vec2 out_pos_in_chamfer_box; layout(location = 4) out nointerpolation vec2 out_chamfer_box_size;
补充说明:
- 和GLSL一致,HLSL/Slang中的整数类型默认就是不插值的,所以
out_uniform_index可以不用显式加nointerpolation,但显式添加也不会有问题 - 浮点类型在HLSL/Slang中默认是线性插值,因此必须显式添加
nointerpolation来匹配GLSL中flat的行为
内容的提问来源于stack exchange,提问作者Zebrafish
相关产品推荐
相关产品推荐

