calligraphy.cl (3057B)
1 #define AA 0.1f 2 #define MAX_DWELL 10000 3 #define BAILOUT 1000000000000.f 4 5 #define LOG2 0.6931471806 6 7 #define SCALE 0.00151f 8 #define XCENTER -0.77235f 9 #define YCENTER -0.10664f 10 11 kernel void render(const int width, const int height, __global float *out) 12 { 13 const int i = get_global_id(0); 14 15 if(i < width * height) 16 { 17 18 float4 pix_col = (float4)(0.f); 19 20 for(float dx = 0.f; dx < 1.f; dx += AA) 21 { 22 for(float dy = 0.f; dy < 1.f; dy += AA) 23 { 24 25 float x = (float)(i % width - width/2 + dx) / height; 26 float y = (float)(i / width - height/2 + dy) / height; 27 28 float cx = x * SCALE + XCENTER; 29 float cy = y * SCALE + YCENTER; 30 31 float zx = 0.f; 32 float zy = 0.f; 33 34 float dzx = 0.0f; 35 float dzy = 0.0f; 36 float tmp; 37 38 int n; 39 float x2, y2; 40 41 42 // Color 43 float arg; 44 float4 st_avg = (float4)(0.f); 45 float4 st_avg2 = (float4)(0.f); 46 47 float highlight = 0.f; 48 49 // Main Loop 50 for(n = 0; n < MAX_DWELL; n++) 51 { 52 arg = atan2(zy, zx); 53 54 st_avg = st_avg.xxyz; 55 st_avg.x += sin( 11.f * arg); 56 57 st_avg2 = st_avg2.xxyz; 58 st_avg2.x += sin( 11.f * arg); 59 60 highlight += exp(-(zx-.5f)*(zx-.5f)*100000.f); 61 62 x2 = zx*zx; 63 y2 = zy*zy; 64 65 if(x2 + y2 > BAILOUT) 66 { 67 break; 68 } 69 70 tmp = 2.0f * (zx*dzx - zy*dzy) + 1.0f; 71 dzy = 2.0f * (zx*dzy + zy*dzx); 72 dzx = tmp; 73 74 zy = (zx+zx)*zy + cy; 75 zx = x2 - y2 + cx; 76 } 77 78 if(n < MAX_DWELL) 79 { 80 float z_mag = sqrt(zx*zx+zy*zy); 81 float dist = z_mag * log(z_mag) / sqrt(dzx*dzx + dzy*dzy); 82 float c1 = pow(min(1.f, 5000000.f*dist), 3.f); 83 84 85 86 float4 fac = (float4)(1.f) / (float4)(n, n-1, n-2, n-3); 87 st_avg = st_avg * fac + (float4)(1.f); 88 st_avg2 = st_avg2 * fac + (float4)(1.f); 89 90 float nu, nu2, nu3, nc; 91 92 nu = 1.f - log2(log(zx*zx + zy*zy)/log(10000000000.f)); 93 nu2 = nu*nu; 94 nu3 = nu2*nu; 95 nc = n + nu; 96 97 float4 interp = (float4)( 98 -nu2 + nu3, 99 nu + 4.f*nu2 - 3.f*nu3, 100 2.f - 5.f*nu2 + 3.f*nu3, 101 -nu + 2.f*nu2 - nu3 102 ); 103 104 float c2 = max(0.f, min(1.f, dot(interp, st_avg)-1.0f)); 105 106 107 float gx = (zx*dzx + zy*dzy)/(dzx*dzx + dzy*dzy); 108 float gy = (zy*dzx - zx*dzy)/(dzx*dzx + dzy*dzy); 109 float l = sqrt(gx*gx + gy*gy); 110 gx /= l; 111 gy /= l; 112 float c3 = 1.0f/sqrt(2.0f) * (gx + gy); 113 114 c3 = max(0.f, c3 + 1.f) * 0.3f + 0.4f; 115 116 117 float c5 = max(0.f, min(1.f, (dot(interp, st_avg2) - 2.f)/(n+nu)*100.f)); 118 c5 = pow(c5, 2.5f); 119 120 //c1=1.f; 121 //c2=1.f; 122 //c3=1.f; 123 124 125 float4 base = (float4)(239, 241, 245, 0)/255.f; 126 float4 base2 = (float4)(172, 176, 190, 0)/255.f; 127 float4 red = (float4)(114, 135, 253, 0)/255.f; 128 float4 text = (float4)(76, 79, 105, 0)/255.f; 129 130 131 float4 color = mix(base2, base, c2*c3); 132 color = mix(text, color, c1); 133 134 pix_col += color; 135 } 136 else 137 { 138 float4 color = (float4)(76, 79, 105, 0)/255.f; 139 pix_col += color; 140 } 141 } 142 } 143 144 out[3*i] = pix_col.x*AA*AA; 145 out[3*i+1] = pix_col.y*AA*AA; 146 out[3*i+2] = pix_col.z*AA*AA; 147 } 148 }