Set up by-hand anti-aliasing for FillShaderWrapper

This commit is contained in:
Grant Sanderson 2023-01-25 17:19:44 -08:00
parent 018b07212f
commit 7609b1db78

View file

@ -275,17 +275,17 @@ class FillShaderWrapper(ShaderWrapper):
super().__init__(ctx, *args, **kwargs) super().__init__(ctx, *args, **kwargs)
size = (2 * DEFAULT_PIXEL_WIDTH, 2 * DEFAULT_PIXEL_HEIGHT) size = (2 * DEFAULT_PIXEL_WIDTH, 2 * DEFAULT_PIXEL_HEIGHT)
self.fill_texture = ctx.texture( texture = ctx.texture(
size=size, size=size,
components=4, components=4,
# Important to make sure floating point (not fixed point) is # Important to make sure floating point (not fixed point) is
# used so that alpha values are not clipped # used so that alpha values are not clipped
dtype='f2', dtype='f2',
) )
# TODO, depth buffer is not really used yet depth_buffer = ctx.depth_renderbuffer(size) # TODO, currently not used
fill_depth = ctx.depth_renderbuffer(size) self.texture_fbo = ctx.framebuffer(texture, depth_buffer)
self.fill_fbo = ctx.framebuffer(self.fill_texture, fill_depth)
self.fill_prog = ctx.program( simple_program = ctx.program(
vertex_shader=''' vertex_shader='''
#version 330 #version 330
@ -301,24 +301,38 @@ class FillShaderWrapper(ShaderWrapper):
#version 330 #version 330
uniform sampler2D Texture; uniform sampler2D Texture;
uniform sampler2D DepthTexture;
uniform float v_nudge;
uniform float h_nudge;
in vec2 v_textcoord; in vec2 v_textcoord;
out vec4 frag_color; out vec4 frag_color;
void main() { void main() {
frag_color = texture(Texture, v_textcoord); // Apply poor man's anti-aliasing
frag_color = abs(frag_color); vec2 tc0 = v_textcoord + vec2(v_nudge, h_nudge);
vec2 tc1 = v_textcoord + vec2(v_nudge, -h_nudge);
vec2 tc2 = v_textcoord + vec2(-v_nudge, h_nudge);
vec2 tc3 = v_textcoord + vec2(-v_nudge, -h_nudge);
frag_color =
0.25 * abs(texture(Texture, tc0)) +
0.25 * abs(texture(Texture, tc1)) +
0.25 * abs(texture(Texture, tc2)) +
0.25 * abs(texture(Texture, tc3));
if(frag_color.a == 0) discard; if(frag_color.a == 0) discard;
//TODO, set gl_FragDepth; //TODO, set gl_FragDepth;
} }
''', ''',
) )
self.fill_prog['Texture'].value = get_texture_id(self.fill_texture) simple_program['Texture'].value = get_texture_id(texture)
# Quarter pixel width/height
simple_program['h_nudge'].value = 0.25 / size[0]
simple_program['v_nudge'].value = 0.25 / size[1]
verts = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) verts = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
self.fill_texture_vao = ctx.simple_vertex_array( self.fill_texture_vao = ctx.simple_vertex_array(
self.fill_prog, simple_program,
ctx.buffer(verts.astype('f4').tobytes()), ctx.buffer(verts.astype('f4').tobytes()),
'texcoord', 'texcoord',
) )
@ -332,8 +346,8 @@ class FillShaderWrapper(ShaderWrapper):
vao.render(moderngl.TRIANGLES) vao.render(moderngl.TRIANGLES)
return return
original_fbo = self.ctx.fbo original_fbo = self.ctx.fbo
self.fill_fbo.clear() self.texture_fbo.clear()
self.fill_fbo.use() self.texture_fbo.use()
self.ctx.blend_func = (moderngl.ONE, moderngl.ONE) self.ctx.blend_func = (moderngl.ONE, moderngl.ONE)
vao.render(self.render_primitive) vao.render(self.render_primitive)
self.ctx.blend_func = moderngl.DEFAULT_BLENDING self.ctx.blend_func = moderngl.DEFAULT_BLENDING