GLSL 计算着色器闪烁块/方块工件

如何解决GLSL 计算着色器闪烁块/方块工件

我正在尝试使用 OpenGL 中的计算着色器编写一个最低限度的 GPU raycaster。我相信光线投射本身是有效的,因为我已经通过光线框交叉算法获得了清晰的边界框轮廓。

然而,在尝试光线三角形相交时,我得到了奇怪的伪影。我的着色器被编程为简单地测试光线三角形交叉点,如果找到交叉点,则将像素着色为白色,否则为黑色。与预期的行为不同,当三角形应该在屏幕上可见时,屏幕会填充黑色和白色方块/块/瓷砖,它们像电视静态一样随机闪烁。正方形最多为 8x8 像素(我的计算着色器块的大小),尽管也有小到单个像素的点。白色块通常位于我的三角形的预期区域内,尽管有时它们也会散布在屏幕底部。

Here is a video of the artifact。在我的完整着色器中,相机可以旋转并且形状看起来更像三角形,但闪烁的伪影是关键问题,并且仍然出现在我从以下最小版本的着色器代码生成的视频中:

layout(local_size_x = 8,local_size_y = 8,local_size_z = 1) in;

uvec2 DIMS = gl_NumWorkGroups.xy*gl_WorkGroupSize.xy;
uvec2 UV = gl_GlobalInvocationID.xy;
vec2 uvf = vec2(UV) / vec2(DIMS);

layout(location = 1,rgba8) uniform writeonly image2D brightnessOut;

struct Triangle
{
    vec3 v0;
    vec3 v1;
    vec3 v2;
};

struct Ray
{
    vec3 origin;
    vec3 direction;
    vec3 inv;
};

// Wikipedia Moller-Trumbore algorithm,GLSL-ified
bool ray_triangle_intersection(vec3 rayOrigin,vec3 rayVector,in Triangle inTriangle,out vec3 outIntersectionPoint)
{
    const float EPSILON = 0.0000001;
    vec3 vertex0 = inTriangle.v0;
    vec3 vertex1 = inTriangle.v1;
    vec3 vertex2 = inTriangle.v2;

    vec3 edge1 = vec3(0.0);
    vec3 edge2 = vec3(0.0);
    vec3 h = vec3(0.0);
    vec3 s = vec3(0.0);
    vec3 q = vec3(0.0);
    float a = 0.0,f = 0.0,u = 0.0,v = 0.0;
    edge1 = vertex1 - vertex0;
    edge2 = vertex2 - vertex0;
    h = cross(rayVector,edge2);
    a = dot(edge1,h);
    // Test if ray is parallel to this triangle.
    if (a > -EPSILON && a < EPSILON)
    {
        return false;
    }

    f = 1.0/a;
    s = rayOrigin - vertex0;
    u = f * dot(s,h);
    if (u < 0.0 || u > 1.0)
    {
        return false;
    }

    q = cross(s,edge1);
    v = f * dot(rayVector,q);
    if (v < 0.0 || u + v > 1.0)
    {
        return false;
    }

    // At this stage we can compute t to find out where the intersection point is on the line.
    float t = f * dot(edge2,q);
    if (t > EPSILON) // ray intersection
    {
        outIntersectionPoint = rayOrigin + rayVector * t;
        return true;
    }
    return false;
}

void main()
{
    // Generate rays by calculating the distance from the eye
    // point to the screen and combining it with the pixel indices
    // to produce a ray through this invocation's pixel
    const float HFOV = (3.14159265359/180.0)*45.0;
    const float WIDTH_PX = 1280.0;
    const float HEIGHT_PX = 720.0;
    float VIEW_PLANE_D = (WIDTH_PX/2.0)/tan(HFOV/2.0);
    vec2 rayXY = vec2(UV) - vec2(WIDTH_PX/2.0,HEIGHT_PX/2.0);

    // Rays have origin at (0,20) and generally point towards (0,-1)
    Ray r;
    r.origin = vec3(0.0,0.0,20.0);
    r.direction = normalize(vec3(rayXY,-VIEW_PLANE_D));
    r.inv = 1.0 / r.direction;
    
    // Triangle in XY plane at Z=0
    Triangle debugTri;
    debugTri.v0 = vec3(-20.0,0.0);
    debugTri.v1 = vec3(20.0,0.0);
    debugTri.v0 = vec3(0.0,40.0,0.0);

    // Test triangle intersection; write 1.0 if hit,else 0.0
    vec3 hitPosDebug = vec3(0.0);
    bool hitDebug = ray_triangle_intersection(r.origin,r.direction,debugTri,hitPosDebug);

    imageStore(brightnessOut,ivec2(UV),vec4(vec3(float(hitDebug)),1.0));
}

我使用法线 sampler2D 和选择映射到屏幕空间的光栅化三角形 UV 将图像渲染为全屏三角形。

这些代码都不应该是时间相关的,我已经尝试了来自各种来源的多种光线三角算法,包括分支和无分支版本,并且都表现出相同的问题,这让我怀疑存在某种内存不一致性行为我不熟悉驱动程序问题,或者我在配置或调度计算时犯的错误(我调度了 8x8x1 块中的 160x90x1 来覆盖我的 1280x720 帧缓冲区纹理)。

我在 SE 和一般互联网上发现了一些类似的问题,例如 this one,但它们似乎几乎完全是由使用未初始化的变量引起的,据我所知,我没有这样做。他们提到在 NSight 调试器中查看时,该模式会继续移动;虽然 RenderDoc 没有这样做,但即使在计算着色器完成之后,图像的内容在绘制调用之间do也会有所不同。例如。在计算绘制调用中检查图像时,存在一种伪影模式,但是当我擦洗到使用我的图像作为输入的后续绘制调用时,尽管没有写入图像,但图像中的模式发生了变化。

我还发现 this 帖子看起来非常相似,但那个帖子似乎也是由未初始化的变量引起的,我再次小心地避免了这种情况。我也无法通过像他们那样调整代码来缓解这个问题。

This 帖子有一个类似的工件,这是内存模型问题,但我没有使用任何共享内存。

我在 GTX 1070 上运行最新的 NVidia 驱动程序 (461.92)。我尝试在计算着色器调度后插入 glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);(以及一些其他屏障类型),我认为这是如果使用 sampler2D 绘制先前由图像加载/存储操作修改的纹理,则使用正确的障碍,但它似乎没有改变任何东西。

我只是尝试在调度调用之前和之后使用 glMemoryBarrier(GL_ALL_BARRIER_BITS); 重新运行它,因此同步似乎不是问题。

很可能问题的原因在我的椅子和键盘之间,但这种问题超出了我通常的着色器调试能力,因为我对 OpenGL 比较陌生。任何想法,将不胜感激!谢谢。

解决方法

我已经解决了这个问题,这(不出所料)只是我自己的一个愚蠢的错误。

观察我的代码片段中的以下几行:

A screenshot of my code,wherein I set the value of the vertices "v0","v1",and "v0" instead of "v2"

这使得我的 v2 顶点完全未初始化。

这个故事的寓意是,如果您遇到与我上面描述的问题类似的问题,并且您上下发誓您已经初始化了所有变量,这一定是驱动程序错误或其他人的错。 . 四次检查你的变量,你可能忘记初始化了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res