光线类型
glsl中的默认光线有以下隐式的内置变量:
raytypeimg {
highp vec3 gl_originimg;
highp vec3 gl_directionimg;
highp rayprogramimg gl_prefixrayprogramimg;
lowp uint gl_sceneimg;
highp float gl_maxdistanceimg;
mediump ivec2 gl_pixelimg;
mediump uint gl_bouncecountimg;
bool gl_isoutgoingimg;
bool gl_flipfacingimg;
bool gl_runprefixprogramimg;
};
通过添加用户自定义变量界定默认光线的类型。例如,阴影光线如下所示:
layout(binding = 0, occlusion_test_always) raytypeimg shadowray {
vec3 colour;
};
调用glscenearrayrayblocksizeimg,可以界定使用的每个光线类型。调用glgetcomponentprogramhandleimg,可以界定每个组件集应该执行的顶点和光线着色器。创建一个像标准顶点或片段着色器一样的光线着色器。
帧着色器
流程的第一部分便是帧着色器。帧着色器是glsl着色器,根据gldispatchraysimg中参数请求的宽度x高度,可以发送零条或多条光线到场景中。注意,不需要通过当前帧着色器中的坐标来积累光线位置,即光线与像素位置不耦合。
layout (rgba8, binding = 0) uniform accumulateonly highp image2d raytracediffuseimage;
layout(max_rays = 1) out;
out shadowray shadowray;
uniform rayprogramimg defaultrayprogram;
void emitshadowray(highp vec3 p, highp vec3 normal, highp vec3 dir, highp float maxdistance, vec3 colour) {
shadowray.gl_originimg = p + depthmodifier*normal;
shadowray.gl_directionimg = dir;
shadowray.gl_prefixrayprogramimg = gl_nullrayprogramimg;
shadowray.gl_sceneimg = uint(gl_dispatchraysidimg);
shadowray.gl_maxdistanceimg = maxdistance;
shadowray.gl_pixelimg = gl_framecoordimg;
shadowray.gl_bouncecountimg = 0u;
shadowray.gl_isoutgoingimg = true;
shadowray.gl_flipfacingimg = false;
shadowray.gl_runprefixprogramimg = false;
shadowray.colour = colour;
emitrayimg(shadowray, defaultrayprogram);
}
void main() {
emitshadowray(vposition, unpackednormal, vnormaliseddirectiontolight, length(vdirectiontolight), vec3(1.0,0.0,0.0));
imageaddimg(raytracediffuseimage, gl_framecoordimg, vec4(0.0,0.0,1.0,0.0));
}
可以看到,帧着色器有一些额外的添加至glsl中。内置的命令是:
gl_dispatchraysidimg 是输入gldispatchraysimg的第一个参数,用于多缓冲。
gl_framecoordimg是目前帧着色器中的坐标。
gl_nullrayprogramimg是无操作程序,用于比较rayprogramimgs。
阴影光线具有可用的隐式光线类变量,如上所示,且每个变量在流程中执行某个函数。在帧着色器中,这些变量通常是可编辑而非可读取。
gl_originimg是发送光线的源头。
gl_directionimg是光线发送的方向。
gl_prefixrayprogramimg是运行交叉光线着色器之前可运行的前缀光线方案。
gl_sceneimg是发送光线的场景id.(如上述glbindscenearraycomponentgroupimg中指定的参数)
gl_maxdistanceimg是光线所能追踪的最大距离,这里不考虑交叉,且运行defaultrayprogram。
gl_pixelimg是光线发送的原始像素。
gl_bouncecountimg是光线当前的反射数。(在帧着色器中通常为0)
gl_isoutgoingimg即光线向外延伸——详细信息在下一篇文章中讨论。
gl_flipfacingimg即是否在下次交叉中翻转表面以再次测试光线。
gl_runprefixprogramimg即是否运行上述的前缀方案。
emitrayimg是发送光线且将光线传输至交叉测试硬件的glsl函数,而imageaddimg是下文即将讨论的累积函数。
shaderayimg也可用。该函数可以在给定的光线方案内对光线着色,且不需要进行交叉测试。
光线着色器
当光线与三角形交叉、光线到达最大距离或想要运行前缀方案时,则启动光线着色器。在帧着色器中,我们仅编写光线变量;而在光线着色器中,我们可以读取光线变量,且发送更多光线时还可以编写光线变量。
layout(binding=0, occlusion_test_always) raytypeimg shadowray {
highp vec3 diffuseobjectcolor;
highp vec3 ambientobjectcolor;
};
layout(binding=1, occlusion_test_never) raytypeimg reflectiveray {
highp vec3 reflectivecolor;
};
layout(rgba8, binding=2) uniform accumulateonly highp image2d reflectionoutput;
in pervertexdata {
highp vec3 vertexnormal;
highp vec2 vertextexcoord;
} vertexdata[];
rayinputhandlerimg(shadowray inputray) {
void main() {
imageaddimg(reflectionoutput, inputray.gl_pixelimg, vec4(inputray.ambientobjectcolor, 0.0));
}
}
rayinputhandlerimg(reflectiveray inputray) {
layout(max_rays=2) out;
out shadowray reflectedshadowray;
out reflectiveray reflectionray;
void main() {
// we interpolate the varyings ourselves
highp vec3 intersectionpoint = interpolateatrayhitimg(gl_in[0].gl_position.xyz, gl_in[1].gl_position.xyz, gl_in[2].gl_position.xyz);
highp vec2 intersectiontexturecoord = interpolateatrayhitimg(vertexdata[0].vertextexcoord.xy, vertexdata[1].vertextexcoord.xy, vertexdata[2].vertextexcoord.xy);
highp vec3 vdirectiontolight = lightdata.vlightposition.xyz - intersectionpoint;
highp vec3 intersectionnormal = interpolateatrayhitimg(vertexdata[0].vertexnormal.xyz, vertexdata[1].vertexnormal.xyz, vertexdata[2].vertexnormal.xyz);
highp vec3 vnormalisednormal = normalize(intersectionnormal);
highp vec4 reflectiontexture = texture(stexture, intersectiontexturecoord);
if (abovereflectionthreshold && inputray.gl_bouncecountimg < number_of_reflection_rays) {
reflectionray.gl_directionimg = reflectiondirection;
reflectionray.gl_originimg = intersectionpoint + reflectiondirectionoffset * reflectionray.gl_directionimg;
reflectionray.gl_maxdistanceimg = inputray.gl_maxdistanceimg;
reflectionray.gl_sceneimg = inputray.gl_sceneimg;
reflectionray.gl_pixelimg = inputray.gl_pixelimg;
reflectionray.gl_bouncecountimg = inputray.gl_bouncecountimg + 1u;
reflectionray.gl_flipfacingimg = // ... set the rest of the ray values
reflectionray.reflectivecolor = reflectedobjectcolor;
emitrayimg(reflectionray, environmentrayprogram);
} else {
// ...
imageaddimg(reflectionoutput, inputray.gl_pixelimg, vec4(environmentaccumulationcolor, 0.0));
}
}
}
光线着色器与通常的opengl es着色器略有差别。首先,其有多个main()入口点。这是因为我们有多类光线。当光线与一些几何图形交叉时,入口点便会执行相应的光线。例如,当阴影光线与附带该光线着色器的几何图形交叉时,将运行第一个main()。而当与反射光线交叉时则运行第二个main()。光线类型通常通过raytypeimg进行分配。在本例中,有两类光线:阴影光线和反射光线。可以看到,在第二个main(),可以发送光线:“layout(max_rays=2) out”。在下一行中还可以看到发送的光线类型。所以main()可以发送两种类型的光线。而对于第一个main(),可以看到,如果该几何图形与阴影光线交叉,则不会发送更多的光线。
接下来便是pervertexdata。这是我们在顶点着色器上编写的变量数据。它被存储在主存中,当运行光线着色器时可以对其进行检索。在三角形上的每个点都具有变量,可以使用interpolateatrayhitimg函数在变量数据上执行重心插值。这与栅格化不同,栅格化主要依靠一些不受我们控制的因素来进行插值。使用该api,我们可以控制插值,这样便可以根据需要来执行不同类型的插值。
在光线发送前先手动增加光线的反射数。这是为了确保我们不会进入一个无限循环中。
在光线着色器中调用imageaddimg。该像素的累积由第二个参数指定。从输入光线中获取像素地址,但这不是必须的,因为光线的来源有很多。
光线限制器
当我们将光线类型设置为occlusion_test_always时,此光线将与其他光线完全不同。这是交叉测试光线的优化。如果光线与任何几何图形相交(即光线限制器,见glcomponentoccluderimg),则删除光线且不做进一步的着色。若光线达到它的最大距离,则仍然可以运行光线着色器。但若光线被任何几何图形遮挡,则这样做有利于测试阴影。在硬件中光线限制器有一个快速路径,这对于开发人员而言是有用的机制。
前缀程序
前缀程序指的是在执行交叉光线着色器之前执行光线着色器。我们可以附加前缀程序至各光线着色器中。假设其使用用于存在距离因素且基于效果的光线,如云层或水的渲染。我们需要了解光线行驶的距离。例如,下图中,我们了解云层与另一交叉对象之间的距离。这时便可以在运行交叉对象着色器之前运行前缀着色器来计算该云层的着色量。
关于这个特征还可以举出更多例子,在有关前缀程序及光线距离选择的文章中我们将做进一步阐述。
混合渲染
光线追踪与基于延迟渲染的powervr拼贴硬件非常匹配。有了像素的本地存储扩展(pls)我们可以使用光栅化来渲染场景,向g缓冲区编写信息并使之保存在本地,再随后在g缓冲区中发出光线追踪命令。目前为止这已经应用到许多技术中,包括软阴影及照明。使用本地内存意味着读写g缓冲区时可以节省内存带宽。更多资讯敬请期待。
sdk演示
sdk团队正在使用源代码以及辅助函数做演示示例,以使光线追踪应用程序的创建更加简单。
光线追踪sdk演示软阴影
性能
未来将贴出更多有关光线追踪性能的文章。我们的powervr sdk性能分析工具pvrtune支持从光线追踪硬件中读取性能计数器。两个新的硬件模块如下图所示。
powervr sdk pvrtune中的光线追踪计数器
更多资讯
目前您可以通过nda访问扩展规范。预计未来我们将公布这一规范。我们的硬件目前用作pcie的测试芯片,其在gdc会议上进行过展示。关于gdc大会上光线追踪展示材料请点击。
IBM开发出突破通信瓶颈的超小型毫米波芯片
最简单的调频话筒电子制作
戴尔:2023 HPC行业的趋势和挑战
苹果推出的动力电池,它的性能怎么样
苹果申请新专利 通过iOS平台蓝牙定位与控制汽车
硬件加速的PowerVR光线追踪API(二)
从MCU的CPU中分流图形处理功能及Chrom-ART模块应用
AI四小龙云从科技科创板递表:拟募集37.5亿元
英特尔推出基于RealSense技术的3D人脸验证解决方案
OPPO带你重新认识平板电脑 在万家灯火阑珊处
海马汽车近年来略显颓势 依旧没有一款拿得出手的电动汽车
免费下载!一款基于Zynq的Harris角点检测算法实现Demo
三星S8发布会:三星S8配置曝光,4大黑科技加持!三星S8三个月无理由退货,买or不买
封测业看景气 上半年有望回升
小米生态链背后的公司阵容
家用监控摄像机安装时可能会遇到什么问题
SF6充气试验变压器充六氟化硫气体
宁德时代紧跟特斯拉步伐,计划在印尼投资50亿美元建锂电池厂
国微思尔芯发布自动原型编译软件Player Pro-7,直击大规模芯片设计痛点
什么是区块链的自然和超自然状态