如何在 glsl 中检测片段是否是多重采样的?

发布于 2024-09-18 18:45:12 字数 91 浏览 2 评论 0原文

是否有任何快速(为了性能)方法可以在 glsl 中检测片段是否已被多重采样,但在第二(轻)通道中使用第一通道渲染的纹理。或者 opengl 如何存储有关多重采样的信息?

Is there any fast(for performance) way to detect in glsl if fragment has been multisampled, but in second(light) pass using textures where been 1st pass rendered. Or how is opengl storing informations about multisampling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-09-25 18:45:12

他们有好几个。通常的方法是检查当前的坐标(gl_FragCoord)是否为(0.5,0.5)。如果是,则意味着您位于多边形的中间:仅采样一次。

它不是,它可能是 4 个(对于 4xMSAA)旋转方角之一:您处于边缘,并且 openGL 检测到仅一个样本是不够的。

另请参阅http://www.opengl.org/pipeline/article/vol003_6/

不过,为了在第二遍中获得此信息,您必须将其存储在 g 缓冲区中。

编辑:这是我刚刚完成的代码片段。在 gtx 470 上使用 1024x1024 4xMSAA 纹理进行测试。

顶点着色器:

    #version 400 core

uniform mat4 MVP;
noperspective centroid out vec2 posCentroid;

layout(location = 0) in vec4 Position;

void main(){    
    gl_Position = MVP * Position;
    posCentroid = (gl_Position.xy / gl_Position.w)*512; // there is a factor two compared to 1024 because normalized coordinates have range [-1,1], not [0,1]
}

片段着色器:

#version 400 core

out vec4 color;
noperspective centroid in  vec2 posCentroid;

void main()
{
    if (abs(fract(posCentroid.x) - 0.5) < 0.01 && abs(fract(posCentroid.y) - 0.5) < 0.01){
        color = vec4(1,0,0,0);
    }else{
        color = vec4(0,1,0,0);
    }
}

边缘为绿色,多边形中心为红色。

对于您原来的问题,我向您推荐这篇文章: http://www.gamasutra .com/view/feature/3591/resolve_your_resolves.php

They are several. The usual one is to check is the current's coordinates (gl_FragCoord) are (0.5, 0.5). If it is, it means that you're in the middle of a polygon : it's sampled only once.

It it's not, it's probably one of the 4 (for 4xMSAA) rotated-square-corners : You're on an edge, and openGL has detected that one sample only isn't enough.

See also http://www.opengl.org/pipeline/article/vol003_6/

In order to have this information in a second pass, you'll have to store it in a g-buffer, though.

EDIT : Here is a code snippet that I've just done. Tested on gtx 470 with a 1024x1024 4xMSAA texture.

Vertex shader :

    #version 400 core

uniform mat4 MVP;
noperspective centroid out vec2 posCentroid;

layout(location = 0) in vec4 Position;

void main(){    
    gl_Position = MVP * Position;
    posCentroid = (gl_Position.xy / gl_Position.w)*512; // there is a factor two compared to 1024 because normalized coordinates have range [-1,1], not [0,1]
}

Fragment shader :

#version 400 core

out vec4 color;
noperspective centroid in  vec2 posCentroid;

void main()
{
    if (abs(fract(posCentroid.x) - 0.5) < 0.01 && abs(fract(posCentroid.y) - 0.5) < 0.01){
        color = vec4(1,0,0,0);
    }else{
        color = vec4(0,1,0,0);
    }
}

Edges are green, center of polygon is red.

For your original question, I recommend you this article : http://www.gamasutra.com/view/feature/3591/resolve_your_resolves.php

や三分注定 2024-09-25 18:45:12

选择像素格式时,多重采样会在上下文创建时激活。之后您将无法关闭或打开它。但是,当渲染到纹理时,无论上下文的设置是什么,OpenGL都不会使用多重采样。

http://www.opengl.org/wiki/Multisampling

Multisampling is activated at contex creation when choosing the pixel format. After that you can't turn it off or on. But when you render to a texture OpenGL will not use multisampling regardless what the setting for the contex is.

http://www.opengl.org/wiki/Multisampling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文