Android|Android OpenGL ES抗锯齿
多重采样MSAA
GLSurfaceView设置多重采样
/**
* 抗锯齿EGLConfigChooser
*
* author weiss
* email kleinminamo@gmail.com
* created 2018/7/3.
*/
public class MSAAConfigChooser implements GLSurfaceView.EGLConfigChooser {
@Override
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {int attribs[] = {
EGL10.EGL_LEVEL, 0,
EGL10.EGL_RENDERABLE_TYPE, 4,// EGL_OPENGL_ES2_BIT
EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
EGL10.EGL_RED_SIZE, 8,
EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_DEPTH_SIZE, 24,
EGL10.EGL_SAMPLE_BUFFERS, 1,
EGL10.EGL_SAMPLES, 4,// 在这里修改MSAA的倍数,4就是4xMSAA,再往上开程序可能会崩
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] configCounts = new int[1];
egl.eglChooseConfig(display, attribs, configs, 1, configCounts);
if (configCounts[0] == 0) {
// Failed! Error handling.
return null;
} else {
return configs[0];
}
}
}
后处理FXAA Shader
#ifndef FXAA_REDUCE_MIN
#define FXAA_REDUCE_MIN(1.0/ 128.0)
#endif
#ifndef FXAA_REDUCE_MUL
#define FXAA_REDUCE_MUL(1.0 / 8.0)
#endif
#ifndef FXAA_SPAN_MAX
#define FXAA_SPAN_MAX8.0
#endif//optimized version for mobile, where dependent
//texture reads can be a bottleneck
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;
mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture2D(tex, v_rgbNW).xyz;
vec3 rgbNE = texture2D(tex, v_rgbNE).xyz;
vec3 rgbSW = texture2D(tex, v_rgbSW).xyz;
vec3 rgbSE = texture2D(tex, v_rgbSE).xyz;
vec4 texColor = texture2D(tex, v_rgbM);
vec3 rgbM= texColor.xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM= dot(rgbM,luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
mediump vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y =((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;
vec3 rgbA = 0.5 * (
texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz +
texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = vec4(rgbA, texColor.a);
else
color = vec4(rgbB, texColor.a);
return color;
}
特别注意用了后处理FBO,多重采样会失效,离屏渲染需要对FBO进行多重采样
FBO
const char* __glExtensions = (const char*)glGetString(GL_EXTENSIONS);
if(strstr(__glExtensions, "GL_IMG_multisampled_render_to_texture"))
{
if(glFramebufferTexture2DMultisampleIMG_ == NULL)
{
glFramebufferTexture2DMultisampleIMG_ = (PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEIMGPROC )eglGetProcAddress ( "glFramebufferTexture2DMultisampleIMG" );
}
}
else if(strstr(__glExtensions, "GL_EXT_multisampled_render_to_texture"))
{
if(glFramebufferTexture2DMultisampleIMG_ == NULL)
{
glFramebufferTexture2DMultisampleIMG_ = (PFNGLFRAMEBUFFERTEXTURE2DMULTISAMPLEIMGPROC )eglGetProcAddress ( "glFramebufferTexture2DMultisampleEXT" );
}
}//glFramebufferTexture2DMultisampleIMG_
if(_multiSample)
{
GL_ASSERT( glFramebufferTexture2DMultisampleIMG_(GL_FRAMEBUFFER, attachment, textureTarget, _renderTargets[index]->getTexture()->getHandle(), 0, 4);
);
} else{
GL_ASSERT( glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, textureTarget, _renderTargets[index]->getTexture()->getHandle(), 0));
}
RBO
const char* __glExtensions = (const char*)glGetString(GL_EXTENSIONS);
if(strstr(__glExtensions, "GL_IMG_multisampled_render_to_texture"))
{
if(glRenderbufferStorageMultisampleIMG_ == NULL)
{
glRenderbufferStorageMultisampleIMG_ = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEIMGPROC )eglGetProcAddress ( "glRenderbufferStorageMultisampleIMG" );
}
}
else if(strstr(__glExtensions, "GL_EXT_multisampled_render_to_texture"))
{
if(glRenderbufferStorageMultisampleIMG_ == NULL)
{
glRenderbufferStorageMultisampleIMG_ = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEIMGPROC )eglGetProcAddress ( "glRenderbufferStorageMultisampleEXT" );
}
}//glRenderbufferStorageMultisampleIMG_
glRenderbufferStorageMultisampleIMG_(GL_RENDERBUFFER, 4,
GL_DEPTH24_STENCIL8, width, height);
// w width, h height.
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 对抗抑郁最好的方法
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)