[canvas, gl] use pre-multiplied alpha in draw shader and when compositing batches

This commit is contained in:
martinfouilleul 2023-02-27 19:50:18 +01:00
parent ac6a5db209
commit 9cb4aedbc5
3 changed files with 13 additions and 5 deletions

View File

@ -140,7 +140,7 @@ void mg_gl_canvas_begin(mg_canvas_backend* interface)
{ {
mg_gl_canvas_backend* backend = (mg_gl_canvas_backend*)interface; mg_gl_canvas_backend* backend = (mg_gl_canvas_backend*)interface;
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
} }
void mg_gl_canvas_end(mg_canvas_backend* interface) void mg_gl_canvas_end(mg_canvas_backend* interface)

View File

@ -365,6 +365,8 @@ const char* glsl_draw =
"\n" "\n"
" int shapeIndex = vertexBuffer.elements[i0].shapeIndex;\n" " int shapeIndex = vertexBuffer.elements[i0].shapeIndex;\n"
" vec4 color = shapeBuffer.elements[shapeIndex].color;\n" " vec4 color = shapeBuffer.elements[shapeIndex].color;\n"
" color.rgb *= color.a;\n"
"\n"
" ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor));\n" " ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor));\n"
"\n" "\n"
" mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0],\n" " mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0],\n"
@ -437,9 +439,11 @@ const char* glsl_draw =
" {\n" " {\n"
" vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1);\n" " vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1);\n"
" vec2 uv = (uvTransform * sampleFP).xy;\n" " vec2 uv = (uvTransform * sampleFP).xy;\n"
" nextColor *= texture(srcTexture, uv);\n" " vec4 texColor = texture(srcTexture, uv);\n"
" texColor.rgb *= texColor.a;\n"
" nextColor *= texColor;\n"
" }\n" " }\n"
" currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor.a*nextColor;\n" " currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor;\n"
" currentShapeIndex[sampleIndex] = shapeIndex;\n" " currentShapeIndex[sampleIndex] = shapeIndex;\n"
" flipCount[sampleIndex] = 1;\n" " flipCount[sampleIndex] = 1;\n"
" }\n" " }\n"

View File

@ -142,6 +142,8 @@ void main()
int shapeIndex = vertexBuffer.elements[i0].shapeIndex; int shapeIndex = vertexBuffer.elements[i0].shapeIndex;
vec4 color = shapeBuffer.elements[shapeIndex].color; vec4 color = shapeBuffer.elements[shapeIndex].color;
color.rgb *= color.a;
ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor)); ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor));
mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0], mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0],
@ -214,9 +216,11 @@ void main()
{ {
vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1); vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1);
vec2 uv = (uvTransform * sampleFP).xy; vec2 uv = (uvTransform * sampleFP).xy;
nextColor *= texture(srcTexture, uv); vec4 texColor = texture(srcTexture, uv);
texColor.rgb *= texColor.a;
nextColor *= texColor;
} }
currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor.a*nextColor; currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor;
currentShapeIndex[sampleIndex] = shapeIndex; currentShapeIndex[sampleIndex] = shapeIndex;
flipCount[sampleIndex] = 1; flipCount[sampleIndex] = 1;
} }