gl_engine: change all shader output premultiplied alpha color

This commit is contained in:
RuiwenTang 2024-03-17 15:37:35 +08:00 committed by Hermet Park
parent d37ed535d1
commit 1e93eb0f49
3 changed files with 34 additions and 34 deletions

View file

@ -84,7 +84,7 @@ bool GlRenderer::sync()
mGpuBuffer->flushToGPU(); mGpuBuffer->flushToGPU();
// Blend function for straight alpha // Blend function for straight alpha
GL_CHECK(glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA)); GL_CHECK(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
GL_CHECK(glEnable(GL_BLEND)); GL_CHECK(glEnable(GL_BLEND));
GL_CHECK(glEnable(GL_SCISSOR_TEST)); GL_CHECK(glEnable(GL_SCISSOR_TEST));
GL_CHECK(glCullFace(GL_FRONT_AND_BACK)); GL_CHECK(glCullFace(GL_FRONT_AND_BACK));
@ -687,7 +687,7 @@ GlRenderPass* GlRenderer::currentPass()
return &mRenderPassStack.back(); return &mRenderPassStack.back();
} }
void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity) void GlRenderer::prepareCmpTask(GlRenderTask* task)
{ {
// we use 1:1 blit mapping since compositor fbo is same size as root fbo // we use 1:1 blit mapping since compositor fbo is same size as root fbo
Array<float> vertices(5 * 4); Array<float> vertices(5 * 4);
@ -700,25 +700,21 @@ void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity)
// left top point // left top point
vertices.push(left); vertices.push(left);
vertices.push(top); vertices.push(top);
vertices.push(opacity);
vertices.push(0.f); vertices.push(0.f);
vertices.push(1.f); vertices.push(1.f);
// left bottom point // left bottom point
vertices.push(left); vertices.push(left);
vertices.push(bottom); vertices.push(bottom);
vertices.push(opacity);
vertices.push(0.f); vertices.push(0.f);
vertices.push(0.f); vertices.push(0.f);
// right top point // right top point
vertices.push(right); vertices.push(right);
vertices.push(top); vertices.push(top);
vertices.push(opacity);
vertices.push(1.f); vertices.push(1.f);
vertices.push(1.f); vertices.push(1.f);
// right bottom point // right bottom point
vertices.push(right); vertices.push(right);
vertices.push(bottom); vertices.push(bottom);
vertices.push(opacity);
vertices.push(1.f); vertices.push(1.f);
vertices.push(0.f); vertices.push(0.f);
@ -734,8 +730,8 @@ void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity)
uint32_t vertexOffset = mGpuBuffer->push(vertices.data, vertices.count * sizeof(float)); uint32_t vertexOffset = mGpuBuffer->push(vertices.data, vertices.count * sizeof(float));
uint32_t indexOffset = mGpuBuffer->push(indices.data, vertices.count * sizeof(uint32_t)); uint32_t indexOffset = mGpuBuffer->push(indices.data, vertices.count * sizeof(uint32_t));
task->addVertexLayout(GlVertexLayout{0, 3, 5 * sizeof(float), vertexOffset}); task->addVertexLayout(GlVertexLayout{0, 2, 4 * sizeof(float), vertexOffset});
task->addVertexLayout(GlVertexLayout{1, 2, 5 * sizeof(float), vertexOffset + 3 * sizeof(float)}); task->addVertexLayout(GlVertexLayout{1, 2, 4 * sizeof(float), vertexOffset + 2 * sizeof(float)});
task->setDrawRange(indexOffset, indices.count); task->setDrawRange(indexOffset, indices.count);
task->setViewport(RenderRegion{ task->setViewport(RenderRegion{
@ -797,7 +793,7 @@ void GlRenderer::endRenderPass(Compositor* cmp)
auto compose_task = self_pass.endRenderPass<GlDrawBlitTask>(program, currentPass()->getFboId()); auto compose_task = self_pass.endRenderPass<GlDrawBlitTask>(program, currentPass()->getFboId());
prepareCmpTask(compose_task, cmp->opacity / 255.f); prepareCmpTask(compose_task);
{ {
uint32_t loc = program->getUniformLocation("uSrcTexture"); uint32_t loc = program->getUniformLocation("uSrcTexture");
@ -819,7 +815,7 @@ void GlRenderer::endRenderPass(Compositor* cmp)
auto task = renderPass.endRenderPass<GlDrawBlitTask>( auto task = renderPass.endRenderPass<GlDrawBlitTask>(
mPrograms[RT_Image].get(), currentPass()->getFboId()); mPrograms[RT_Image].get(), currentPass()->getFboId());
prepareCmpTask(task, 1.f); prepareCmpTask(task);
// matrix buffer // matrix buffer
{ {

View file

@ -90,7 +90,7 @@ private:
GlRenderPass* currentPass(); GlRenderPass* currentPass();
void prepareCmpTask(GlRenderTask* task, float opacity); void prepareCmpTask(GlRenderTask* task);
void endRenderPass(Compositor* cmp); void endRenderPass(Compositor* cmp);
GLint mTargetFboId = 0; GLint mTargetFboId = 0;

View file

@ -43,7 +43,7 @@ const char* COLOR_FRAG_SHADER = TVG_COMPOSE_SHADER(
void main() \n void main() \n
{ \n { \n
vec4 uColor = uColorInfo.solidColor; \n vec4 uColor = uColorInfo.solidColor; \n
FragColor = uColor; \n FragColor = vec4(uColor.rgb * uColor.a, uColor.a); \n
}); });
const char* GRADIENT_VERT_SHADER = TVG_COMPOSE_SHADER( const char* GRADIENT_VERT_SHADER = TVG_COMPOSE_SHADER(
@ -171,7 +171,7 @@ void main()
\n \n
vec3 noise = 8.0 * uGradientInfo.nStops[1] * ScreenSpaceDither(pos); \n vec3 noise = 8.0 * uGradientInfo.nStops[1] * ScreenSpaceDither(pos); \n
vec4 finalCol = vec4(color.xyz + noise, color.w); \n vec4 finalCol = vec4(color.xyz + noise, color.w); \n
FragColor = finalCol; \n FragColor = vec4(finalCol.rgb * finalCol.a, finalCol.a); \n
}); });
std::string STR_RADIAL_GRADIENT_VARIABLES = TVG_COMPOSE_SHADER( std::string STR_RADIAL_GRADIENT_VARIABLES = TVG_COMPOSE_SHADER(
@ -200,7 +200,7 @@ void main()
\n \n
vec3 noise = 8.0 * uGradientInfo.nStops[1] * ScreenSpaceDither(pos); \n vec3 noise = 8.0 * uGradientInfo.nStops[1] * ScreenSpaceDither(pos); \n
vec4 finalCol = vec4(color.xyz + noise, color.w); \n vec4 finalCol = vec4(color.xyz + noise, color.w); \n
FragColor = finalCol; \n FragColor = vec4(finalCol.rgb * finalCol.a, finalCol.a); \n
}); });
std::string STR_LINEAR_GRADIENT_FRAG_SHADER = std::string STR_LINEAR_GRADIENT_FRAG_SHADER =
@ -255,20 +255,20 @@ const char* IMAGE_FRAG_SHADER = TVG_COMPOSE_SHADER(
vec4 color = texture(uTexture, uv); \n vec4 color = texture(uTexture, uv); \n
vec4 result = color; \n vec4 result = color; \n
if (uColorInfo.format == 1) { /* FMT_ARGB8888 */ \n if (uColorInfo.format == 1) { /* FMT_ARGB8888 */ \n
result.r = color.b; \n
result.g = color.g; \n
result.b = color.r; \n
result.a = color.a; \n
} else if (uColorInfo.format == 2) { /* FMT_ABGR8888S */ \n
result = vec4(color.rgb * color.a, color.a); \n
} else if (uColorInfo.format == 3) { /* FMT_ARGB8888S */ \n
result.r = color.b * color.a; \n result.r = color.b * color.a; \n
result.g = color.g * color.a; \n result.g = color.g * color.a; \n
result.b = color.b * color.a; \n result.b = color.r * color.a; \n
result.a = color.a; \n
} else if (uColorInfo.format == 2) { /* FMT_ABGR8888S */ \n
result = color; \n
} else if (uColorInfo.format == 3) { /* FMT_ARGB8888S */ \n
result.r = color.b; \n
result.g = color.g; \n
result.b = color.b; \n
result.a = color.a; \n result.a = color.a; \n
} \n } \n
float opacity = float(uColorInfo.opacity) / 255.0; \n float opacity = float(uColorInfo.opacity) / 255.0; \n
FragColor = vec4(result.rgb, result.a * opacity); \n FragColor = result * opacity; \n
} \n } \n
); );
@ -298,8 +298,7 @@ void main() { \n
vec4 srcColor = texture(uSrcTexture, vUV); \n vec4 srcColor = texture(uSrcTexture, vUV); \n
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
\n \n
FragColor = vec4(srcColor.rgb, \n FragColor = srcColor * maskColor.a; \n
srcColor.a * maskColor.a); \n
} \n } \n
); );
@ -315,8 +314,7 @@ void main() { \n
vec4 srcColor = texture(uSrcTexture, vUV); \n vec4 srcColor = texture(uSrcTexture, vUV); \n
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
\n \n
FragColor = vec4(srcColor.rgb, \n FragColor = srcColor *(1.0 - maskColor.a); \n
srcColor.a * (1.0 - maskColor.a)); \n
} \n } \n
); );
@ -332,7 +330,12 @@ void main() {
vec4 srcColor = texture(uSrcTexture, vUV); \n vec4 srcColor = texture(uSrcTexture, vUV); \n
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
\n \n
FragColor = srcColor * (0.299 * maskColor.r + 0.587 * maskColor.g + 0.114 * maskColor.b); \n if (maskColor.a > 0.000001) { \n
maskColor = vec4(maskColor.rgb / maskColor.a, maskColor.a); \n
} \n
\n
FragColor = \n
srcColor * (0.299 * maskColor.r + 0.587 * maskColor.g + 0.114 * maskColor.b) * maskColor.a; \n
} \n } \n
); );
@ -349,6 +352,7 @@ void main() {
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
\n \n
float luma = (0.299 * maskColor.r + 0.587 * maskColor.g + 0.114 * maskColor.b); \n float luma = (0.299 * maskColor.r + 0.587 * maskColor.g + 0.114 * maskColor.b); \n
luma *= maskColor.a; \n
FragColor = srcColor * (1.0 - luma); \n FragColor = srcColor * (1.0 - luma); \n
} \n } \n
); );
@ -384,10 +388,11 @@ void main() { \n
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
float a = srcColor.a - maskColor.a; \n float a = srcColor.a - maskColor.a; \n
\n \n
if (a <= 0.0) { \n if (a < 0.0 || srcColor.a == 0.0) { \n
FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n
} else { \n } else { \n
FragColor = vec4(srcColor.rgb, srcColor.a * a); \n vec3 srcRgb = srcColor.rgb / srcColor.a; \n
FragColor = vec4(srcRgb * a, a); \n
} \n } \n
} \n } \n
); );
@ -404,9 +409,8 @@ void main() { \n
vec4 srcColor = texture(uSrcTexture, vUV); \n vec4 srcColor = texture(uSrcTexture, vUV); \n
vec4 maskColor = texture(uMaskTexture, vUV); \n vec4 maskColor = texture(uMaskTexture, vUV); \n
\n \n
float intAlpha = srcColor.a * maskColor.a; \n
\n \n
FragColor = vec4(maskColor.rgb, maskColor.a * intAlpha); \n FragColor = maskColor * srcColor.a; \n
} \n } \n
); );
@ -427,9 +431,9 @@ void main() { \n
if (da == 0.0) { \n if (da == 0.0) { \n
FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n FragColor = vec4(0.0, 0.0, 0.0, 0.0); \n
} else if (da > 0.0) { \n } else if (da > 0.0) { \n
FragColor = vec4(srcColor.rgb, srcColor.a * da); \n FragColor = srcColor * da; \n
} else { \n } else { \n
FragColor = vec4(maskColor.rgb, maskColor.a * (-da)); \n FragColor = maskColor * (-da); \n
} \n } \n
} \n } \n
); );