多采样和片段着色器 [英] multisampling and fragment shader

查看:120
本文介绍了多采样和片段着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多重采样似乎不适用于片段着色器生成的片段. 在下面的示例中,片段着色器用于生成棋盘程序纹理. 正方形的外边缘经过适当的抗锯齿处理,但过程纹理的内边缘却没有进行抗锯齿处理.

Multisampling does not seem to work for fragments generated by a fragment shader. In the example below, the fragment shader is used to produce a check-board procedural texture. The outer edges of the square are properly antialiased, but the inner edges of the procedural texture are not.

片段着色器仅按像素评估吗? 还是给定像素的每个片段的纹理坐标都相同?

Is the fragment shader evaluated only per pixel? Or are the texture coordinates the same for each fragment of a given pixel?

下面是代码,并且图像显示了其输出(请注意,过程边缘位于—白色和灰色正方形-不抗锯齿,而几何边缘(在黑白/灰色之间)是:

Below is the code and the image shows its output (notice that the procedural edges —between white and gray square— are not antialiased, whereas geometry edges —between black and white/gray— are):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


# imports ####################################################################

import sys

from OpenGL.GLUT import *
from OpenGL.GL import *


# display ####################################################################

def reshape(width, height):
    """window reshape callback."""
    glViewport(0, 0, width, height)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    r = float(min(width, height))
    w, h = 2*width/r, 2*height/r
    glOrtho(-w, w, -h, h, -1, 1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glRotate(45, 0, 0, 1)

def display():
    """window redisplay callback."""
    glClear(GL_COLOR_BUFFER_BIT)
    glBegin(GL_TRIANGLE_STRIP)
    for x in [-1, 1]:
        for y in [-1, 1]:
            glTexCoord(x, y)
            glVertex(x, y)
    glEnd()
    glutSwapBuffers()


# setup ######################################################################

glutInit(sys.argv)
glutInitDisplayString(b"rgba double samples=4")
glutInitWindowSize(100, 100)
glutCreateWindow(sys.argv[0].encode())

glutReshapeFunc(reshape)
glutDisplayFunc(display)

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(shader, """
    void main() {
        vec2 c = gl_TexCoord[0].xy;
        vec4 color = gl_Color;
        if(c.x*c.y < 0.) color.a *= .5;
        gl_FragColor = color;
    }
""")
glCompileShader(shader)
program = glCreateProgram()
glAttachShader(program, shader)
glLinkProgram(program)
glUseProgram(program)

glutMainLoop()  

推荐答案

sample_shading 需要扩展:在标准的多样本渲染中,允许实现为每个样本分配相同的颜色和纹理坐标值,然后允许进行优化,其中着色器仅评估一次,然后分配给已确定当前正在栅格化的图元所覆盖."

The sample_shading extension is needed: "In standard multisample rendering, an implementation is allowed to assign the same color and texture coordinate values to each sample, which then allows the optimization where the shader is only evaluated once and then distributed to the samples that have been determined to be covered by the primitive currently being rasterized."

这篇关于多采样和片段着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆