PaintLab / PixelFarm

From Vectors to (sub) Pixels, C# 2D Rendering Library
Other
156 stars 20 forks source link

developing SMAA shader #20

Open prepare opened 6 years ago

prepare commented 6 years ago

SMAA for real time AA shader

see http://www.iryoku.com/smaa/

https://github.com/iryoku/smaa


Aims: provider real-time AA like these pictures

I test our lion images with the original (iryoku) program(SMAA-DX10-v2.8.exe) I think the results are impressive and fit my need.

This is test image. menu_01

pic 1: test image

target_1

pic 2: disable SMAA


target_2

pic 3: enable SMAA


menu_02_no_smaa1

pic 4: no SMAA, our lcd-effect text look clear


menu_02_custom_setting

pic 5: custom setting SMAA, our lcd-effect text look blur, but the lion is good


menu_02_smaa_ultra_setting

pic 6: with preset SMAA (ultra) , color edge detection


small_lion2

pic 7: right side (AA) image is post SMAA filter of the lion on the left

small_lion1

pic 8: small lion, rendered with Agg (software rendering), with AA

At small side, our Agg is finer.


so , Let implement this!

prepare commented 6 years ago

Implementation

The 2 images from WebGL port of SMAA (see more at https://github.com/mrdoob/three.js/blob/dev/examples/js/shaders/SMAAShader.js, https://github.com/mrdoob/three.js/blob/dev/examples/js/postprocessing/SMAAPass.js)

getareatexture

pic 1: getTextureArea(), png, depth 24 bits

getsearchtexture

pic 2: getSearchTexture(), png, depth 8 bits

search_text_a01

pic3 : modified version of pic 2, rgba32 bits

prepare commented 6 years ago

Apply SMAA's edge detection filter (step 1)

lines pic 1: test input data

p2 pic 2: just draw the img on GLES2 surface, no other filter


the original code, this steps alpha value always = 0.0 (so you will not see any thing, you will see just a red rect.)

so I modified the shader for debugging purpose.

edge_detection_step

pic 3: On GLES2, SMAA edge detection

on red background,

black => discard color yellow line => full edges (ignore original weight, just an opaque yellow, ) with alpha = 1.0,


lets discard color as original, and let the edges have weight as original (vary yellow) with alpha =1.0

edge_detection_step_p2

pic 4: GLES2, SMAA edge detection


and when turn it to the original code

edge_detection_step_p3

pic 5: just a red rect

prepare commented 6 years ago

since pic 3-4-5 have red background.

here, the left one is stage 1 output image without red background compare with the original output(right).

smaa_n_01

pic 6: you can see the red and green pixels, but this is not correct

prepare commented 6 years ago

Early experiment with errs : (

early_experiment_with_errs2

pic 1: attemp1, err

early_experiment_with_errs3

pic 2: pic 1 (above) zoom


early_experiment_with_errs

attempt2

attempt3

pic 3: lion, err


attempt3

pic 4: attemp 4

attempt5

pic 5: zoom on pic 4, (1) anti-alias at left leg of the lion compare to the original alias(2)

You can see that there are some anti-alias results. We come into the nearly correct way now!


attempt6

attempt6

_pic 6: fix SMAA_AREATEXSELECT, still err

developing ...

prepare commented 6 years ago

attempt 2: step1 edge detect I think it is pixel perfect !

lion_side_by_side

pic 4: Color Edge detection, PixelFarm (left) vs original (right)


lion_side_by_side2

pic 5: pic 4 zoom in, lion's forehead , PixelFarm (top) vs original (bottom)


lion_side_by_side3

pic 6: pic 4 zoom in, text part, PixelFarm (top) vs original (bottom)

prepare commented 5 years ago

I'm working on this ...

prepare commented 5 years ago

Attempt 3, not correct. smaa_01_bugs

prepare commented 5 years ago

smaa_01_bugs_edges

pic 1: edges

smaa_01_bugs_weight

pic 2: blend weight, not correct

top-to-left_bottom is ok smaa_01_bugs_partial_aa

errs

test_4

attemp 5, errs

prepare commented 5 years ago

MLAA's L, Z, U shapes

from RESHETOV A. : Morphological antialiasing. In Proceedings of the Conference on High Performance Graphics 2009

mlaa_fig2


mlaa_fig3


mlaa_from_smaa_paper_fig3 from SMAA paper

prepare commented 5 years ago

Area Texture

Calculates the area for a given pattern and distances to the left and to the right, biased by an offset:

areatex_notation

calarea_ortho


Orthogonal Pattern 0

o_p0

ortho_0_offset_-0 375 -0.375 ortho_0_offset_-0 25 -0.25 ortho_0_offset_-0 125 -0.125

ortho_0_offset_0 +0

ortho_0_offset_0 125 0.125

ortho_0_offset_0 25 +0.25

ortho_0_offset_0 375 +0.375


Orthogonal Pattern 1

mlaa_patterns

o_p1

ortho_1_offset_-0 375 -0.375 ortho_1_offset_-0 25 -0.25 ortho_1_offset_-0 125 -0.125

ortho_1_offset_0 +0

ortho_1_offset_0 125 +0.125

ortho_1_offset_0 25 +0.25

ortho_1_offset_0 375 +0.375


Orthogonal Pattern 2

mlaa_patterns02_1

o_p2

ortho_2_offset_-0 375 -0.375 ortho_2_offset_-0 25 -0.25

ortho_2_offset_-0 125 -0.125

ortho_2_offset_0 +0

ortho_2_offset_0 125 +0.125

ortho_2_offset_0 25 +0.25

ortho_2_offset_0 375 +0.375


Orthogonal Pattern 3

mlaa_patterns02_1

o_p3

ortho_3_offset_-0 375 -0.375 ortho_3_offset_-0 25 -0.25 ortho_3_offset_-0 125 -0.125 ortho_3_offset_0 +0 ortho_3_offset_0 125 +0.125 ortho_3_offset_0 25 +0.25 ortho_3_offset_0 375 +0.375


prepare commented 5 years ago

Orthogonal Pattern 4

mlaa_patterns

o_p4

ortho_4_offset_-0 375 -0.375 ortho_4_offset_-0 25 -0.25 ortho_4_offset_-0 125 -0.125 ortho_4_offset_0 +0 ortho_4_offset_0 125 +0.125 ortho_4_offset_0 25 +0.25 ortho_4_offset_0 375 +0.375


Orthogonal Pattern 5

mlaa_patterns

o_p5

ortho_5_offset_-0 375 -0.375 ortho_5_offset_-0 25 -0.25 ortho_5_offset_-0 125 -0.125 ortho_5_offset_0 +0 ortho_5_offset_0 125 +0.125 ortho_5_offset_0 25 +0.25

ortho_5_offset_0 375 +0.375


Orthogonal Pattern 6

mlaa_patterns

o_p6

ortho_6_offset_-0 375 -0.375 ortho_6_offset_-0 25 -0.25 ortho_6_offset_-0 125 -0.125 ortho_6_offset_0 +0

ortho_6_offset_0 125 +0.125

ortho_6_offset_0 25 +0.25

ortho_6_offset_0 375 +0.375


Orthogonal Pattern 7

mlaa_patterns

o_p7

ortho_7_offset_-0 375 -0.375 ortho_7_offset_-0 25 -0.25 ortho_7_offset_-0 125 -0.125 ortho_7_offset_0 +0 ortho_7_offset_0 125 +0.125 ortho_7_offset_0 25 +0.25 ortho_7_offset_0 375 +0.375


Orthogonal Pattern 8

mlaa_patterns

o_p8

ortho_8_offset_-0 375 -0.375 ortho_8_offset_-0 25 -0.25 ortho_8_offset_-0 125 -0.125 ortho_8_offset_0 +0 ortho_8_offset_0 125 +0.125 ortho_8_offset_0 25 +0.25 ortho_8_offset_0 375 +0.375

prepare commented 5 years ago

Orthogonal Pattern 9

mlaa_patterns

o_p9

ortho_9_offset_-0 375 -0.375 ortho_9_offset_-0 25 -0.25 ortho_9_offset_-0 125 -0.125

ortho_9_offset_0 +0 ortho_9_offset_0 125 +0.125 ortho_9_offset_0 25 +0.25

ortho_9_offset_0 375 +0.375


Orthogonal Pattern 10

mlaa_patterns

o_p10

ortho_10_offset_-0 375 -0.375 ortho_10_offset_-0 25 -0.25 ortho_10_offset_-0 125 -0.125 ortho_10_offset_0 +0 ortho_10_offset_0 125 +0.125 ortho_10_offset_0 25 +0.25 ortho_10_offset_0 375 +0.375


Orthogonal Pattern 11

mlaa_patterns

o_p11

ortho_11_offset_-0 375 -0.375 ortho_11_offset_-0 25 -0.25 ortho_11_offset_-0 125 -0.125 ortho_11_offset_0 +0 ortho_11_offset_0 125 +0.125 ortho_11_offset_0 25 +0.25 ortho_11_offset_0 375 +0.375


Orthogonal Pattern 12

mlaa_patterns

o_p12

ortho_12_offset_-0 375 -0.375 ortho_12_offset_-0 25 -0.25 ortho_12_offset_-0 125 -0.125 ortho_12_offset_0 +0 ortho_12_offset_0 125 +0.125 ortho_12_offset_0 25 +0.25 ortho_12_offset_0 375 +0.375

prepare commented 5 years ago

Orthogonal Pattern 13

mlaa_patterns

o_p13

ortho_13_offset_-0 375 -0.375

ortho_13_offset_-0 25 -0.25

ortho_13_offset_-0 125 -0.125

ortho_13_offset_0 +0

ortho_13_offset_0 125 +0.125

ortho_13_offset_0 25 +0.25

ortho_13_offset_0 375 +0.375


Orthogonal Pattern 14

mlaa_patterns

o_p14

ortho_14_offset_-0 375 -0.375

ortho_14_offset_-0 25 -0.25

ortho_14_offset_-0 125 -0.125

ortho_14_offset_0 +0

ortho_14_offset_0 125 +0.125

ortho_14_offset_0 25 +0.25

ortho_14_offset_0 375 +0.375


Orthogonal Pattern 15

mlaa_patterns

o_p15

ortho_15_offset_-0 375 -0.375

ortho_15_offset_-0 25 -0.25

ortho_15_offset_-0 125 -0.125

ortho_15_offset_0 +0

ortho_15_offset_0 125 +0.125

ortho_15_offset_0 25 +0.25

ortho_15_offset_0 375 +0.375

prepare commented 5 years ago

Diagonal Patterns

diagonal_pattern_smaa_paper from SMAA paper, fig 8 , Diagonal patterns map (left) and their precomputed area texture (right)

Diagonal Pattern 0

diagonal_pattern_smaa_paper2

d_p0

diag_0_offset_ -0 25 0 25 -0.25, 0.25

diag_0_offset_ -0 125 0 125 -0.125, 0.125

diag_0_offset_ 0 0 0,0

diag_0_offset_ 0 125 -0 125 0.125,-0.125

diag_0_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 1

diagonal_pattern_smaa_paper2

d_p1

diag_1_offset_ -0 25 0 25 -0.25, 0.25

diag_1_offset_ -0 125 0 125 -0.125, 125

diag_1_offset_ 0 0 0,0

diag_1_offset_ 0 125 -0 125 0.125,-0.125

diag_1_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 2

diagonal_pattern_smaa_paper2

d_p2

diag_2_offset_ -0 25 0 25 0.25,0.25

diag_2_offset_ -0 125 0 125 -0.125,0.125 diag_2_offset_ 0 0 0.0

diag_2_offset_ 0 125 -0 125 0.125,-0.125

diag_2_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 3

diagonal_pattern_smaa_paper2

d_p3

diag_3_offset_ -0 25 0 25 -0.25,0.25

diag_3_offset_ -0 125 0 125 -0.125,0.125 diag_3_offset_ 0 0 0,0

diag_3_offset_ 0 125 -0 125 0.125,-0.125

diag_3_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 4

diagonal_pattern_smaa_paper2

d_p4

diag_4_offset_ -0 25 0 25 -0.25,0.25

diag_4_offset_ -0 125 0 125 -0.125,0.125

diag_4_offset_ 0 0 0,0

diag_4_offset_ 0 125 -0 125 0.125,-0.125

diag_4_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 5

diagonal_pattern_smaa_paper2

d_p5

diag_5_offset_ -0 25 0 25 -0.25,0.25

diag_5_offset_ -0 125 0 125 -0.125,0.125

diag_5_offset_ 0 0 0,0

diag_5_offset_ 0 125 -0 125 0.125,-0.125

diag_5_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 6

diagonal_pattern_smaa_paper2

d_p6

diag_6_offset_ -0 25 0 25 -0.25,0.25

diag_6_offset_ -0 125 0 125 -0.125,0.125

diag_6_offset_ 0 0 0,0 diag_6_offset_ 0 125 -0 125 0.125,-0.125

diag_6_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 7

diagonal_pattern_smaa_paper2

d_p7

diag_7_offset_ -0 25 0 25 -0.25,0.25

diag_7_offset_ -0 125 0 125 -0.125,-0.125

diag_7_offset_ 0 0 0,0

diag_7_offset_ 0 125 -0 125 0.125,-0.125

diag_7_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 8

diagonal_pattern_smaa_paper2

d_p8

diag_8_offset_ -0 25 0 25 -0.25,0.25

diag_8_offset_ -0 125 0 125 -0.125,0.125

diag_8_offset_ 0 0 0,0

diag_8_offset_ 0 125 -0 125 0.125,-0.125

diag_8_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 9

diagonal_pattern_smaa_paper2

d_p9

diag_9_offset_ -0 25 0 25 -0.25,0.25

diag_9_offset_ -0 125 0 125 -0.125,0.125

diag_9_offset_ 0 0 0,0

diag_9_offset_ 0 125 -0 125 0.125,-0.125

diag_9_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 10

diagonal_pattern_smaa_paper2

d_p10

diag_10_offset_ -0 25 0 25 -0.25,0.25

diag_10_offset_ -0 125 0 125 -0.125,0.125

diag_10_offset_ 0 0 0,0

diag_10_offset_ 0 125 -0 125 0.125,-0.125

diag_10_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 11

diagonal_pattern_smaa_paper2

d_p11

diag_11_offset_ -0 25 0 25 -0.25,0.25

diag_11_offset_ -0 125 0 125 -0.125,0.125

diag_11_offset_ 0 0 0,0

diag_11_offset_ 0 125 -0 125 0.125,-0.125

diag_11_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 12

diagonal_pattern_smaa_paper2

d_p12

diag_12_offset_ -0 25 0 25 -0.25,0.25

diag_12_offset_ -0 125 0 125 -0.125,0.125

diag_12_offset_ 0 0 0,0

diag_12_offset_ 0 125 -0 125 0.125,-0.125

diag_12_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 13

diagonal_pattern_smaa_paper2

d_p13

diag_13_offset_ -0 25 0 25 -0.25,0.25

diag_13_offset_ -0 125 0 125 -0.125,0.125

diag_13_offset_ 0 0 0,0

diag_13_offset_ 0 125 -0 125 0.125,-0.125

diag_13_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 14

diagonal_pattern_smaa_paper2

d_p14

diag_14_offset_ -0 25 0 25 -0.25,0.25

diag_14_offset_ -0 125 0 125 -0.125,0.125

diag_14_offset_ 0 0 0,0

diag_14_offset_ 0 125 -0 125 0.125,-0.125

diag_14_offset_ 0 25 -0 25 0.25,-0.25


Diagonal Pattern 15

diagonal_pattern_smaa_paper2

d_p15

diag_15_offset_ -0 25 0 25 -0.25,0.25

diag_15_offset_ -0 125 0 125 -0.125,0.125

diag_15_offset_ 0 0 0,0

diag_15_offset_ 0 125 -0 125 0.125,-0.125

diag_15_offset_ 0 25 -0 25 0.25,-0.25

prepare commented 5 years ago

Orthogonal Pattern Location

ortho_location0

pic 1: offset 0, 16 patterns with location

ortho_location2 pic 2: left, original and right my re-arranged to show to changes

ortho_location4 pic 3: zoom upper part, offset -0.375,-0.25,-0.125

ortho_location3 pic 4: zoom lower part, offset +0.125, +0.25, +0.375

prepare commented 5 years ago

search_tex1_1

pic 4: bilinear func


search_tex5

pic 5: This dict returns which edges are active for a certain bilinear fetch. (it's the reverse lookup of the bilinear function), see pic 5, red point => sampling position


prepare commented 5 years ago

Checking texture coordinates

tex_coord1_1

pic 8: observing , the nature of texture coord


tex_coord_px_diff

pic 9: observing , texture coord is +0.5,0.5 from grid coord


tex_coord_offset_back_0 5_0 5

_pic 10: texture coord offset back -0.5,-0.5 compare diff with floor(texcoord), diff nearly 0


tex_coord_offset_forward_0 5_0 5

_pic 11: texture coord offset back +0.5,+0.5 compare diff with floor(texcoord),, diff nearly 1

prepare commented 5 years ago

Texture examples

box3 pic 1: original bmp


box_03_edges pic 2: result of step 1 (find edges)


offset_study2_3

pic 3: pic 2, various offset

prepare commented 5 years ago

X-offset only, Zoom In

edges_step pic 4 :from SMAA step 1(edge detection)

offset_x_0 125

pic 5: offset x +0.125 and -0.125


offset_x0 125_z2

pic 6: zoom in, observe diff at arrow marks

A = 255/255 => 1
B = 223/255 => 0.8745 C = B =E=G=I

D= A= F =H

N=Q= S =A = 255/255 => 1
M=O=P=R = 223/255 => 0.8745


when we offset sampling point to left side 1/8 of pixel the leftmost pixel loss 'color energy' = 1/8 pixel remaining = 7/8 of original *so remaining = (7/8)255 = 0.875 * 255 => 233.125***


notes: When we offset sampling point X to +0.125 side (to right side) => the px take more right-side color (eg black) into itself.

When we offset sampling point X to -0.125 side (to left side) => the px take more left-side color (eg black) into itself.

prepare commented 5 years ago

Y-offset only, Zoom In

edges_step pic 7 : from step 1, edge filter

offset_y0 125_v5

pic 8: zoom in

notes: When we offset sampling point Y to +0.125 side (to lower side) => the px take more lower -side color (eg black) into itself.

When we offset sampling point X to -0.125 side (to upper side) => the px take more upper -side color (eg black) into itself.


Analysis on vertical axis

offset_y_0 125 pic 9: left img=> offset=(+0.0px,+0.125px), right img => offset =(+0.0px,-0.125px)

offset_y0 125_vertical_axis_0 125 pic 10: zoom, left img=> offset=(+0.0px,+0.125px), right img => offset =(+0.0px,-0.125px)

A, C= 223/255 => 0.8745 B, D = 255/255 => 1

N, P, R = 223/255 => 0.8745 M, O, Q = 255/255 => 1

notes: When we offset sampling point Y to +0.125 side (to lower side) => the px take more lower -side color (eg black) into itself.

When we offset sampling point X to -0.125 side (to upper side) => the px take more upper -side color (eg black) into itself.


when we offset sampling point to top side 1/8 of pixel the topmost pixel loss 'color energy' = 1/8 pixel remaining = 7/8 of original *so remaining = (7/8)255 = 0.875 * 255 => 233.125***


prepare commented 5 years ago

X Offset only, -0.25 px

offset_x_-0 25 pic 11: zoom

B = 255/255 = 1 A , C , D = 191/255 = 0.749

so remaining = (3/4)255 = 0.75 255 => 191.25

prepare commented 5 years ago

Y Offset only -0.125

offset_y_0 125_v6 pic 12: zoom

A = 223/255 => 0.8745

so remaining = (7/8) 255 = 0.875 255 => 223.125

prepare commented 5 years ago

X offset -0.25px and Y offset -0.125 px

offset_x_-0 25_y-0 125

pic 13: offset x -0.25px and offset y -0.125px

A = 167/255 => 0.6549 B = 167/255 => 0.6549 C = 223/255 => 0.8745 D = 223/255 => 0.8745 E = 167/255 => 0.6549 F = 191/255 => 0.7490 G= 175/255 => 0.6863


Lets do some calculation at A pixel

It value 167/255 can be calculated from bilinear interpolation

1). see pic 11, only X offset -0.25px => A pixel value= (3/4) *255 = 191.25
then bring 191.25 to 2) below

  1. see pic 12, only Y offset -0.125px => (7/8) * 191.25 => 167.34375

so A = 167 :)

OR A= (3/4) (7/8) 255
= (0.65625) * 255
= 167.34375


if we use 'normalized' pixel value (where value range starts from 0.0 to 1.0) in this case, A pixel value = (3 / 4 ) * (7 / 8)= 0.65625

if you look at pic 5 (above), 0.65625 is here, exact the same as real specimen.

example1


Think back...

If you sampling texture at position offset (-0.25px,-0.125px) and you get sampling value= 0.65625

you can assume that you are at the Left-Upper corner

prepare commented 5 years ago

Z shape and its area

edge_step1_3


Sampling around Z-shape

z_shape01

prepare commented 5 years ago

Z shape and its area

horizontal_lines2

pic 17: z-shapes


z_shape02

pic 18: area under the line

prepare commented 5 years ago

U shape and its area

horizontal_lines_u_shapes