我試圖通過片段着色器來做我自己的自定義glBlendFunc,但是,我的解決方案比原生glBlendFunc慢很多,即使他們做了精確的混合函數。自定義glBlendFunc比本機慢很多
我想知道如果有人有任何建議,如何以更有效的方式做到這一點。
我的解決方案的工作是這樣的:
void draw(fbo fbos[2], render_item item)
{
// fbos[0] is the render target
// fbos[1] is the previous render target used to read "background" to blend against in shader
// Both fbos have exactly the same content, however they need to be different since we can't both read and write to the same texture. The texture we render to needs to have the entire content since we might not draw geometry everywhere.
fbos[0]->attach(); // Attach fbo
fbos[1]->bind(1); // Bind as texture 1
render(item);
glCopyTexSubImage2D(...); // copy from fbos[0] to fbos[1], fbos[1] == fbos[0]
}
fragment.glsl
vec4 blend_color(vec4 fore)
{
vec4 back = texture2D(background, gl_TexCoord[1].st); // background is read from texture "1"
return vec4(mix(back.rgb, fore.rgb, fore.a), back.a + fore.a);
}
NV_texture_barrier使我能夠同時渲染和讀取相同的紋理,正確嗎? – ronag
@ronag:排序。最好是閱讀擴展規範,但一般的要點是,只要你不是從同一個地方做的,就可以讀取和寫入相同的紋理。並且你適當地使用屏障。 –