source: trunk/src/gui/painting/qpaintengine_d3d.fx@ 127

Last change on this file since 127 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 16.0 KB
Line 
1bool g_mCosmeticPen;
2int4 g_mChannel;
3float2 g_mMaskOffset;
4int2 g_mMaskSize;
5float4x4 g_mMaskProjection;
6float4x4 g_mViewProjection;
7float4x4 g_mTransformation;
8texture g_mAAMask;
9texture g_mTexture;
10int g_mBrushMode;
11float g_mFocalDist;
12
13#define M_PI 3.14159265358979323846
14
15sampler PixmapSampler = sampler_state
16{
17 texture = <g_mTexture>;
18 MIPFILTER = NONE;
19 MINFILTER = LINEAR;
20 MAGFILTER = LINEAR;
21};
22
23sampler TextSampler = sampler_state
24{
25 texture = <g_mTexture>;
26 MIPFILTER = NONE;
27 MINFILTER = POINT;
28 MAGFILTER = POINT;
29};
30
31sampler AAMaskSampler = sampler_state
32{
33 texture = <g_mAAMask>;
34 AddressU = WRAP;
35 AddressV = WRAP;
36 AddressW = WRAP;
37 MIPFILTER = NONE;
38 MINFILTER = POINT;
39 MAGFILTER = POINT;
40};
41
42struct VS_FULL
43{
44 float4 Position : POSITION;
45 float4 Diffuse : COLOR0;
46 float4 TexCoords0 : TEXCOORD0;
47 float4 TexCoords1 : TEXCOORD1;
48};
49
50VS_FULL TrapezoidVS( float4 Position : POSITION,
51 float4 Diffuse : COLOR0,
52 float4 TexCoords0 : TEXCOORD0,
53 float4 TexCoords1 : TEXCOORD1)
54{
55 VS_FULL Output;
56
57 float a = (TexCoords1.x * Position.x) + (TexCoords1.z * (1.0 - Position.x) ); // left or right a
58 float b = (TexCoords1.y * Position.x) + (TexCoords1.w * (1.0 - Position.x) ); // left or right b
59 float d = 1.0 - (Position.x * 2);
60
61 Position.x = (a * Position.y + b) + ( sqrt( abs(a * a) ) * d );
62 //Position.x += step(abs(a), 0) * d;
63 Position.x += (0.5 * d);
64
65 Output.Position = mul(Position, g_mMaskProjection);
66 Output.Diffuse = Diffuse;
67 Output.TexCoords0 = TexCoords0;
68 Output.TexCoords1 = TexCoords1;
69
70 return Output;
71}
72
73struct PS_OUTPUT
74{
75 float4 Color : COLOR0;
76};
77
78PS_OUTPUT TrapezoidPS(VS_FULL In, float2 pixelPos : VPOS)
79{
80 PS_OUTPUT Out;
81
82 float top = max(pixelPos.y - 0.5, In.TexCoords0.x);
83 float bottom = min(pixelPos.y + 0.5, In.TexCoords0.y);
84
85 float area = bottom - top;
86
87 float left = pixelPos.x - 0.5;
88 float right = pixelPos.x + 0.5;
89
90 // use line equations to compute intersections of left/right edges with top/bottom of truncated pixel
91 // vecX: x = (left, top), y = (left, bottom), z = (right, top), w = (right, bottom)
92 float4 vecX = In.TexCoords1.xxzz * float2(top, bottom).xyxy + In.TexCoords1.yyww;
93
94 float2 invA = In.TexCoords0.zw;
95
96 // transform right line to left to be able to use same calculations for both
97 vecX.zw = 2 * pixelPos.x - vecX.zw;
98
99 float2 topX = float2(vecX.x, vecX.z);
100 float2 bottomX = float2(vecX.y, vecX.w);
101
102 // transform lines such that top intersection is to the right of bottom intersection
103 float2 topXTemp = max(topX, bottomX);
104 float2 bottomXTemp = min(topX, bottomX);
105
106 // make sure line slope reflects mirrored lines
107 invA = lerp(invA, -invA, step(topX, bottomX));
108
109 float2 vecLeftRight = float2(left, right);
110
111 // compute the intersections of the lines with the left and right edges of the pixel
112 // intersectY: x = (left_line, left), y = (left_line, right), z = (right_line, left), w = (right_line, right)
113 float4 intersectY = top + (vecLeftRight.xyxy - topXTemp.xxyy) * invA.xxyy;
114
115 float2 temp = lerp(area - 0.5 * (right - bottomXTemp) * (bottom - intersectY.yw), // left < bottom < right < top
116 (0.5 * (topXTemp + bottomXTemp) - left) * area, // left < bottom < top < right
117 step(topXTemp, right));
118
119 float2 excluded = 0.5 * (intersectY.xz - top) * (topXTemp - left); // bottom < left < top < right
120
121 excluded = lerp(0.5 * (intersectY.yw + intersectY.xz) - top, // bottom < left < right < top
122 excluded, step(topXTemp, right));
123
124 excluded = lerp(temp, // left < bottom < right (see calculation of temp)
125 excluded, step(bottomXTemp, left));
126
127 excluded = lerp(float2(area, area), // right < bottom < top
128 excluded, step(bottomXTemp, right));
129
130 excluded *= step(left, topXTemp);
131
132 float result = (area - excluded.x - excluded.y) * step(top, bottom);
133 Out.Color.r = result * g_mChannel[0];
134 Out.Color.g = result * g_mChannel[1];
135 Out.Color.b = result * g_mChannel[2];
136 Out.Color.a = result * g_mChannel[3];
137
138 return Out;
139}
140
141VS_FULL ViewProjectionVS( float4 Position : POSITION,
142 float4 Diffuse : COLOR0,
143 float4 TexCoords0 : TEXCOORD0,
144 float4 TexCoords1 : TEXCOORD1)
145{
146 VS_FULL Output;
147
148 Output.Position = mul(Position, g_mTransformation);
149 Output.Position = mul(Output.Position, g_mViewProjection);
150 Output.Diffuse = Diffuse;
151 Output.TexCoords0 = TexCoords0;
152 Output.TexCoords1 = TexCoords1;
153
154 return Output;
155}
156
157PS_OUTPUT DirectMaskPS(VS_FULL In, float2 pixelPos : VPOS)
158{
159 PS_OUTPUT Out;
160 Out.Color = In.Diffuse;
161
162 float2 maskcoords = ( (pixelPos + g_mMaskOffset) - 0.5 ) / g_mMaskSize;
163 float2 clipcoords = (pixelPos - 0.5) / g_mMaskSize;
164
165 float4 c = tex2D(AAMaskSampler, maskcoords.xy) * Out.Color.a;
166 Out.Color.a = c.r * g_mChannel[0];
167 Out.Color.a += c.g * g_mChannel[1];
168 Out.Color.a += c.b * g_mChannel[2];
169 Out.Color.a += c.a * g_mChannel[3];
170
171 return Out;
172}
173
174PS_OUTPUT MaskPS(VS_FULL In, float2 pixelPos : VPOS)
175{
176 PS_OUTPUT Out;
177
178 if (g_mBrushMode == 1) {
179 float x = In.TexCoords0.x;
180 float y = In.TexCoords0.y;
181 x = x - int(x);
182 y = y - int(y);
183 Out.Color = tex2D(PixmapSampler, float2(x, y));
184 Out.Color.a = Out.Color.a * In.Diffuse.a;
185 } else if (g_mBrushMode == 2) {
186 Out.Color = tex1D(PixmapSampler, In.TexCoords0.x);
187 } else if (g_mBrushMode == 3) {
188 float t = atan2(In.TexCoords0.y, -In.TexCoords0.x) / (2 * M_PI);
189 Out.Color = tex1D(PixmapSampler, t + 0.5);
190 } else if (g_mBrushMode == 4) {
191 float2 tc = float2(In.TexCoords0.x, abs(In.TexCoords0.y));
192 float a = (tc.x - g_mFocalDist) / tc.y;
193 float b = g_mFocalDist;
194
195 float A = 1 + (a * a);
196 float B = 2.0 * a * b;
197 float C = (b * b) - 1;
198
199 float y = (-B + sqrt(B*B - 4.0*A*C)) / (2.0*A);
200 Out.Color = tex1D(PixmapSampler, (tc.y / y) );
201 } else if (g_mBrushMode == 5) {
202 Out.Color = tex2D(PixmapSampler, In.TexCoords0.xy);
203 Out.Color = Out.Color * In.Diffuse;
204 } else {
205 Out.Color = In.Diffuse;
206 }
207
208 float2 maskcoords = ( (pixelPos + g_mMaskOffset) - 0.5 ) / g_mMaskSize;
209
210 float4 c = tex2D(AAMaskSampler, maskcoords.xy) * Out.Color.a;
211 Out.Color.a = c.r * g_mChannel[0];
212 Out.Color.a += c.g * g_mChannel[1];
213 Out.Color.a += c.b * g_mChannel[2];
214 Out.Color.a += c.a * g_mChannel[3];
215
216 return Out;
217}
218
219struct VS_NORMAL
220{
221 float4 Position : POSITION;
222 float4 Diffuse : COLOR0;
223 float4 TexCoords : TEXCOORD0;
224};
225
226VS_NORMAL MaskProjectionVS(VS_NORMAL In)
227{
228 VS_NORMAL Output;
229
230 Output.Position = mul(In.Position, g_mMaskProjection);
231 Output.Diffuse = In.Diffuse;
232 Output.TexCoords = In.TexCoords;
233
234 return Output;
235}
236
237float4 DirectSimplePS(float4 Color : COLOR0) : COLOR0
238{
239 return Color;
240}
241
242float4 SimplePS(float4 Color : COLOR0, float4 TexCoords : TEXCOORD0) : COLOR0
243{
244 if (g_mBrushMode == 1) {
245 float opacity = Color.a;
246 float x = TexCoords.x;
247 float y = TexCoords.y;
248 x = x - int(x);
249 y = y - int(y);
250 Color = tex2D(PixmapSampler, float2(x, y));
251 Color.a = Color.a * opacity;
252 } else if (g_mBrushMode == 2) {
253 Color = tex1D(PixmapSampler, TexCoords.x);
254 } else if (g_mBrushMode == 3) {
255 float t = atan2(TexCoords.y, -TexCoords.x) / (2 * M_PI);
256 Color = tex1D(PixmapSampler, t + 0.5);
257 } else if (g_mBrushMode == 4) {
258 float2 tc = float2(TexCoords.x, abs(TexCoords.y));
259 float a = (tc.x - g_mFocalDist) / tc.y;
260 float b = g_mFocalDist;
261
262 float A = 1 + (a * a);
263 float B = 2.0 * a * b;
264 float C = (b * b) - 1;
265
266 float y = (-B + sqrt(B*B - 4.0*A*C)) / (2.0*A);
267 Color = tex1D(PixmapSampler, (tc.y / y) );
268 } else if (g_mBrushMode == 5) {
269 Color = tex2D(PixmapSampler, TexCoords.xy) * Color;
270 }
271
272 return Color;
273}
274
275float4 TextPS(float4 Color : COLOR0, float4 TexCoords : TEXCOORD0) : COLOR0
276{
277 Color.a *= tex2D(TextSampler, TexCoords.xy).a;
278 return Color;
279}
280
281float4 ClearTypePS(float4 Color : COLOR0, float4 TexCoords : TEXCOORD0) : COLOR0
282{
283// if (g_mUsePixmap) {
284// float4 MaskColor = tex2D(PixmapSampler, TexCoords.xy);
285// Color = float4(1.0, 0.0, 0.0, 1.0);
286// Color.a = (1 - MaskColor.a) + MaskColor.a * Color.a;
287// Color.r = (1.0 - MaskColor.r) + (MaskColor.r * Color.r);
288// Color.g = (1.0 - MaskColor.g) + (MaskColor.g * Color.g);
289// Color.b = (1.0 - MaskColor.b) + (MaskColor.b * Color.b);
290// Color = MaskColor;
291 return tex2D(PixmapSampler, TexCoords.xy);
292}
293
294VS_NORMAL NoTxAliasedVS(VS_NORMAL In)
295{
296 VS_NORMAL Output;
297
298 Output.Position = mul(In.Position, g_mViewProjection);
299 Output.Diffuse = In.Diffuse;
300 Output.TexCoords = In.TexCoords;
301
302 return Output;
303}
304
305VS_NORMAL AliasedVS(VS_NORMAL In)
306{
307 VS_NORMAL Output;
308
309 Output.Position = mul(In.Position, g_mTransformation);
310 Output.Position = mul(Output.Position, g_mViewProjection);
311 Output.Diffuse = In.Diffuse;
312 Output.TexCoords = In.TexCoords;
313
314 return Output;
315}
316
317VS_NORMAL AliasedLinesVS(VS_NORMAL In)
318{
319 VS_NORMAL Output;
320
321 float4 start = float4(In.Position.x, In.Position.y, 0.5, In.Position.w);
322 float4 end = float4(In.TexCoords.z, In.TexCoords.w, 0.5, In.Position.w);
323 if (g_mCosmeticPen) {
324 start = mul(start, g_mTransformation);
325 end = mul(end, g_mTransformation);
326 }
327
328 float2 line_vec = end - start;
329 float2 vec = normalize(line_vec);
330 float2 norm = float2(-vec.y, vec.x);
331
332 float pen_width = In.Position.z;
333 norm = norm * pen_width * 0.5;
334 vec = vec * pen_width * 0.5;
335
336 Output.Position.w = In.Position.w;
337 Output.Position.x = start.x + (vec.x * In.TexCoords.x);
338 Output.Position.x = Output.Position.x + (norm.x * In.TexCoords.y);
339 Output.Position.x = Output.Position.x + (line_vec.x * step(0, In.TexCoords.x));
340 Output.Position.y = start.y + (vec.y * In.TexCoords.x);
341 Output.Position.y = Output.Position.y + (norm.y * In.TexCoords.y);
342 Output.Position.y = Output.Position.y + (line_vec.y * step(0, In.TexCoords.x));
343 Output.Position.z = 0.5;
344
345 if (!g_mCosmeticPen) {
346 Output.Position = mul(Output.Position, g_mTransformation);
347 }
348 Output.Position = mul(Output.Position, g_mViewProjection);
349
350 Output.Diffuse = In.Diffuse;
351 Output.TexCoords = In.TexCoords;
352
353 return Output;
354}
355
356
357technique Antialiased
358{
359 pass PASS_AA_CREATEMASK
360 {
361 StencilEnable = False;
362 ZWriteEnable = False;
363 ColorWriteEnable = 0x0f;
364 ZEnable = False;
365
366 SrcBlend = One;
367 DestBlend = One;
368
369 VertexShader = compile vs_3_0 TrapezoidVS();
370 PixelShader = compile ps_3_0 TrapezoidPS();
371 }
372
373 pass PASS_AA_DRAW
374 {
375 StencilEnable = False;
376 ZFunc = Greater;
377 ZWriteEnable = False;
378 ZEnable = True;
379 ColorWriteEnable = 0x0f;
380
381 VertexShader = compile vs_3_0 ViewProjectionVS();
382 PixelShader = compile ps_3_0 MaskPS();
383 }
384
385 pass PASS_AA_DRAW_DIRECT
386 {
387 StencilEnable = False;
388 ZFunc = Greater;
389 ZEnable = True;
390 ZWriteEnable = False;
391 ColorWriteEnable = 0x0f;
392
393 VertexShader = compile vs_3_0 ViewProjectionVS();
394 PixelShader = compile ps_3_0 DirectMaskPS();
395 }
396}
397
398technique Aliased
399{
400 pass PASS_STENCIL_ODDEVEN
401 {
402 TwoSidedStencilMode = False;
403 StencilEnable = True;
404 StencilPass = Invert;
405 StencilFunc = Always;
406 ColorWriteEnable = 0;
407
408 ZEnable = False;
409 ZWriteEnable = False;
410
411 VertexShader = compile vs_1_1 NoTxAliasedVS();
412 PixelShader = compile ps_2_0 DirectSimplePS();
413 }
414
415 pass PASS_STENCIL_WINDING
416 {
417 TwoSidedStencilMode = True;
418 StencilEnable = True;
419 StencilRef = 0;
420 StencilMask = 0xFFFFFFFF;
421
422 CCW_StencilPass = Incr;
423 CCW_StencilFunc = Always;
424
425 StencilPass = Decr;
426 StencilFunc = Always;
427
428 ColorWriteEnable = 0;
429
430 ZEnable = False;
431 ZWriteEnable = False;
432
433 VertexShader = compile vs_1_1 NoTxAliasedVS();
434 PixelShader = compile ps_2_0 DirectSimplePS();
435 }
436
437 pass PASS_STENCIL_DRAW
438 {
439 TwoSidedStencilMode = False;
440 StencilEnable = True;
441 StencilFunc = NotEqual;
442 StencilMask = 0xFFFFFFFF;
443 StencilRef = 0;
444 StencilPass = Zero;
445 StencilFail = Zero;
446 StencilZFail = Zero;
447
448 ColorWriteEnable = 0x0f;
449 ZEnable = True;
450 ZWriteEnable = False;
451 ZFunc = Greater;
452
453 VertexShader = compile vs_1_1 AliasedVS();
454 PixelShader = compile ps_2_0 SimplePS();
455 }
456
457 pass PASS_STENCIL_DRAW_DIRECT
458 {
459 TwoSidedStencilMode = False;
460 StencilEnable = True;
461 StencilFunc = NotEqual;
462 StencilMask = 0xFFFFFFFF;
463 StencilRef = 0;
464 StencilPass = Zero;
465 StencilFail = Zero;
466 StencilZFail = Zero;
467
468 ColorWriteEnable = 0x0f;
469 ZEnable = True;
470 ZWriteEnable = False;
471 ZFunc = Greater;
472
473 VertexShader = compile vs_1_1 AliasedVS();
474 PixelShader = compile ps_2_0 DirectSimplePS();
475 }
476
477 pass PASS_STENCIL_CLIP
478 {
479 TwoSidedStencilMode = False;
480 StencilEnable = True;
481 StencilFunc = NotEqual;
482 StencilMask = 0xFFFFFFFF;
483 StencilRef = 0;
484 StencilPass = Zero;
485 StencilFail = Zero;
486 StencilZFail = Zero;
487
488 ColorWriteEnable = 0;
489 ZEnable = True;
490 ZWriteEnable = True;
491 ZFunc = Always;
492
493 VertexShader = compile vs_1_1 NoTxAliasedVS();
494 PixelShader = compile ps_2_0 DirectSimplePS();
495 }
496
497 pass PASS_STENCIL_NOSTENCILCHECK
498 {
499 StencilEnable = False;
500
501 ZEnable = True;
502 ZWriteEnable = False;
503 ZFunc = Greater;
504
505 ColorWriteEnable = 0x0f;
506
507 SrcBlend = SrcAlpha;
508 DestBlend = InvSrcAlpha;
509
510 VertexShader = compile vs_1_1 AliasedVS();
511 PixelShader = compile ps_2_0 SimplePS();
512 }
513
514 pass PASS_STENCIL_NOSTENCILCHECK_DIRECT
515 {
516 StencilEnable = False;
517
518 ZEnable = True;
519 ZWriteEnable = False;
520 ZFunc = Greater;
521
522 ColorWriteEnable = 0x0f;
523
524 SrcBlend = SrcAlpha;
525 DestBlend = InvSrcAlpha;
526
527 VertexShader = compile vs_1_1 AliasedVS();
528 PixelShader = compile ps_2_0 DirectSimplePS();
529 }
530
531 pass PASS_TEXT
532 {
533 StencilEnable = False;
534
535 ZEnable = True;
536 ZWriteEnable = False;
537 ZFunc = Greater;
538
539 ColorWriteEnable = 0x0f;
540
541 SrcBlend = SrcAlpha;
542 DestBlend = InvSrcAlpha;
543
544 VertexShader = compile vs_1_1 AliasedVS();
545 PixelShader = compile ps_2_0 TextPS();
546 }
547
548 pass PASS_CLEARTYPE_TEXT
549 {
550 StencilEnable = False;
551
552 ZEnable = True;
553 ZWriteEnable = False;
554 ZFunc = Greater;
555
556 ColorWriteEnable = 0x0f;
557
558// SrcBlend = SrcAlpha;
559// DestBlend = InvSrcAlpha;
560
561// SrcBlend = DestColor;
562// DestBlend = Zero;
563 SrcBlend = BlendFactor;
564 DestBlend = InvSrcColor;
565
566// SrcBlend = Zero;
567// DestBlend = SrcColor;
568
569// SrcBlend = One;
570// DestBlend = Zero;
571
572 VertexShader = compile vs_3_0 AliasedVS();
573 PixelShader = compile ps_3_0 ClearTypePS();
574 }
575
576 pass PASS_ALIASED_LINES
577 {
578 TwoSidedStencilMode = False;
579 StencilEnable = True;
580 StencilPass = Invert;
581 StencilFunc = Always;
582 ColorWriteEnable = 0;
583
584 ZEnable = False;
585 ZWriteEnable = False;
586
587 VertexShader = compile vs_1_1 AliasedLinesVS();
588 PixelShader = compile ps_2_0 DirectSimplePS();
589 }
590
591 pass PASS_ALIASED_LINES_DIRECT
592 {
593 StencilEnable = False;
594
595 ZEnable = True;
596 ZWriteEnable = False;
597 ZFunc = Greater;
598
599 ColorWriteEnable = 0x0f;
600
601 SrcBlend = SrcAlpha;
602 DestBlend = InvSrcAlpha;
603
604 VertexShader = compile vs_1_1 AliasedLinesVS();
605 PixelShader = compile ps_2_0 DirectSimplePS();
606 }
607}
608
Note: See TracBrowser for help on using the repository browser.