Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: cc/shader.cc

Issue 12086036: cc: Clamp texture coordinates in all tile shaders (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snowscale
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/shader.h" 5 #include "cc/shader.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h" 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3 D.h"
10 10
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 "s_texture", 356 "s_texture",
357 }; 357 };
358 int locations[1]; 358 int locations[1];
359 359
360 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shade rUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex) ; 360 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shade rUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex) ;
361 361
362 m_samplerLocation = locations[0]; 362 m_samplerLocation = locations[0];
363 DCHECK(m_samplerLocation != -1); 363 DCHECK(m_samplerLocation != -1);
364 } 364 }
365 365
366 FragmentTexClampOpaqueBinding::FragmentTexClampOpaqueBinding()
367 : m_samplerLocation(-1)
368 {
369 }
370
371 void FragmentTexClampOpaqueBinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
372 {
373 static const char* shaderUniforms[] = {
374 "s_texture",
375 "fragmentTexClamp",
376 };
377 int locations[2];
378
379 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shade rUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex) ;
380
381 m_samplerLocation = locations[0];
382 m_fragmentTexClampLocation = locations[2];
383 DCHECK(m_samplerLocation != -1);
384 }
385
366 std::string FragmentShaderRGBATexFlipVaryingAlpha::getShaderString() const 386 std::string FragmentShaderRGBATexFlipVaryingAlpha::getShaderString() const
367 { 387 {
368 return SHADER( 388 return SHADER(
369 precision mediump float; 389 precision mediump float;
370 varying vec2 v_texCoord; 390 varying vec2 v_texCoord;
371 varying float v_alpha; 391 varying float v_alpha;
372 uniform sampler2D s_texture; 392 uniform sampler2D s_texture;
373 void main() 393 void main()
374 { 394 {
375 vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texC oord.y)); 395 vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texC oord.y));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 uniform sampler2D s_texture; 433 uniform sampler2D s_texture;
414 uniform float alpha; 434 uniform float alpha;
415 void main() 435 void main()
416 { 436 {
417 vec4 texColor = texture2D(s_texture, v_texCoord); 437 vec4 texColor = texture2D(s_texture, v_texCoord);
418 gl_FragColor = texColor * alpha; 438 gl_FragColor = texColor * alpha;
419 } 439 }
420 ); 440 );
421 } 441 }
422 442
443 std::string FragmentShaderRGBATexAlpha::getShaderString() const
444 {
445 return SHADER(
446 precision mediump float;
447 varying vec2 v_texCoord;
448 uniform sampler2D s_texture;
449 uniform float alpha;
450 uniform vec4 fragmentTexClamp;
451 void main()
452 {
453 vec2 texCoord = clamp(v_texCoord, fragmentTexClamp.xy, fragmentTexCl amp.zw);
454 vec4 texColor = texture2D(s_texture, texCoord);
455 gl_FragColor = texColor * alpha;
456 }
457 );
458 }
459
460
423 std::string FragmentShaderRGBATexVaryingAlpha::getShaderString() const 461 std::string FragmentShaderRGBATexVaryingAlpha::getShaderString() const
424 { 462 {
425 return SHADER( 463 return SHADER(
426 precision mediump float; 464 precision mediump float;
427 varying vec2 v_texCoord; 465 varying vec2 v_texCoord;
428 varying float v_alpha; 466 varying float v_alpha;
429 uniform sampler2D s_texture; 467 uniform sampler2D s_texture;
430 void main() 468 void main()
431 { 469 {
432 vec4 texColor = texture2D(s_texture, v_texCoord); 470 vec4 texColor = texture2D(s_texture, v_texCoord);
(...skipping 26 matching lines...) Expand all
459 "varying vec2 v_texCoord;\n" 497 "varying vec2 v_texCoord;\n"
460 "varying float v_alpha;\n" 498 "varying float v_alpha;\n"
461 "uniform sampler2DRect s_texture;\n" 499 "uniform sampler2DRect s_texture;\n"
462 "void main()\n" 500 "void main()\n"
463 "{\n" 501 "{\n"
464 " vec4 texColor = texture2DRect(s_texture, v_texCoord);\n" 502 " vec4 texColor = texture2DRect(s_texture, v_texCoord);\n"
465 " gl_FragColor = texColor * v_alpha;\n" 503 " gl_FragColor = texColor * v_alpha;\n"
466 "}\n"; 504 "}\n";
467 } 505 }
468 506
469 std::string FragmentShaderRGBATexOpaque::getShaderString() const 507 std::string FragmentShaderRGBATexClampOpaque::getShaderString() const
470 { 508 {
471 return SHADER( 509 return SHADER(
472 precision mediump float; 510 precision mediump float;
473 varying vec2 v_texCoord; 511 varying vec2 v_texCoord;
474 uniform sampler2D s_texture; 512 uniform sampler2D s_texture;
513 uniform vec4 fragmentTexClamp;
475 void main() 514 void main()
476 { 515 {
477 vec4 texColor = texture2D(s_texture, v_texCoord); 516 vec2 texCoord = clamp(v_texCoord, fragmentTexClamp.xy, fragmentTexCl amp.zw);
517 vec4 texColor = texture2D(s_texture, texCoord);
478 gl_FragColor = vec4(texColor.rgb, 1.0); 518 gl_FragColor = vec4(texColor.rgb, 1.0);
479 } 519 }
480 ); 520 );
481 } 521 }
482 522
483 std::string FragmentShaderRGBATex::getShaderString() const 523 std::string FragmentShaderRGBATex::getShaderString() const
484 { 524 {
485 return SHADER( 525 return SHADER(
486 precision mediump float; 526 precision mediump float;
487 varying vec2 v_texCoord; 527 varying vec2 v_texCoord;
488 uniform sampler2D s_texture; 528 uniform sampler2D s_texture;
489 void main() 529 void main()
490 { 530 {
491 gl_FragColor = texture2D(s_texture, v_texCoord); 531 gl_FragColor = texture2D(s_texture, v_texCoord);
492 } 532 }
493 ); 533 );
494 } 534 }
495 535
496 std::string FragmentShaderRGBATexSwizzleAlpha::getShaderString() const 536 std::string FragmentShaderRGBATexClampSwizzleAlpha::getShaderString() const
497 { 537 {
498 return SHADER( 538 return SHADER(
499 precision mediump float; 539 precision mediump float;
500 varying vec2 v_texCoord; 540 varying vec2 v_texCoord;
501 uniform sampler2D s_texture; 541 uniform sampler2D s_texture;
502 uniform float alpha; 542 uniform float alpha;
503 void main() 543 void main()
504 { 544 {
505 vec4 texColor = texture2D(s_texture, v_texCoord); 545 vec4 texColor = texture2D(s_texture, v_texCoord);
506 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha; 546 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha;
507 } 547 }
508 ); 548 );
509 } 549 }
510 550
511 std::string FragmentShaderRGBATexSwizzleOpaque::getShaderString() const 551 std::string FragmentShaderRGBATexClampSwizzleOpaque::getShaderString() const
512 { 552 {
513 return SHADER( 553 return SHADER(
514 precision mediump float; 554 precision mediump float;
515 varying vec2 v_texCoord; 555 varying vec2 v_texCoord;
516 uniform sampler2D s_texture; 556 uniform sampler2D s_texture;
557 uniform vec4 fragmentTexClamp;
517 void main() 558 void main()
518 { 559 {
519 vec4 texColor = texture2D(s_texture, v_texCoord); 560 vec2 texCoord = clamp(v_texCoord, fragmentTexClamp.xy, fragmentTexCl amp.zw);
561 vec4 texColor = texture2D(s_texture, texCoord);
520 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, 1.0); 562 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, 1.0);
521 } 563 }
522 ); 564 );
523 } 565 }
524 566
525 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA() 567 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA()
526 : m_samplerLocation(-1) 568 : m_samplerLocation(-1)
527 , m_alphaLocation(-1) 569 , m_alphaLocation(-1)
528 , m_edgeLocation(-1) 570 , m_edgeLocation(-1)
529 { 571 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0); 609 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
568 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0); 610 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
569 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min (a4, a6) * min(a5, a7)); 611 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min (a4, a6) * min(a5, a7));
570 } 612 }
571 ); 613 );
572 } 614 }
573 615
574 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding() 616 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding()
575 : m_samplerLocation(-1) 617 : m_samplerLocation(-1)
576 , m_alphaLocation(-1) 618 , m_alphaLocation(-1)
577 , m_fragmentTexTransformLocation(-1) 619 , m_fragmentTexClampLocation(-1)
578 , m_edgeLocation(-1) 620 , m_edgeLocation(-1)
579 { 621 {
580 } 622 }
581 623
582 void FragmentTexClampAlphaAABinding::init(WebGraphicsContext3D* context, unsigne d program, bool usingBindUniform, int* baseUniformIndex) 624 void FragmentTexClampAlphaAABinding::init(WebGraphicsContext3D* context, unsigne d program, bool usingBindUniform, int* baseUniformIndex)
583 { 625 {
584 static const char* shaderUniforms[] = { 626 static const char* shaderUniforms[] = {
585 "s_texture", 627 "s_texture",
586 "alpha", 628 "alpha",
587 "fragmentTexTransform", 629 "fragmentTexClamp",
588 "edge", 630 "edge",
589 }; 631 };
590 int locations[4]; 632 int locations[4];
591 633
592 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shade rUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex) ; 634 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shade rUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex) ;
593 635
594 m_samplerLocation = locations[0]; 636 m_samplerLocation = locations[0];
595 m_alphaLocation = locations[1]; 637 m_alphaLocation = locations[1];
596 m_fragmentTexTransformLocation = locations[2]; 638 m_fragmentTexClampLocation = locations[2];
597 m_edgeLocation = locations[3]; 639 m_edgeLocation = locations[3];
598 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_fragmentTexTran sformLocation != -1 && m_edgeLocation != -1); 640 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_fragmentTexClam pLocation != -1 && m_edgeLocation != -1);
599 } 641 }
600 642
601 std::string FragmentShaderRGBATexClampAlphaAA::getShaderString() const 643 std::string FragmentShaderRGBATexClampAlphaAA::getShaderString() const
602 { 644 {
603 return SHADER( 645 return SHADER(
604 precision mediump float; 646 precision mediump float;
605 varying vec2 v_texCoord; 647 varying vec2 v_texCoord;
606 uniform sampler2D s_texture; 648 uniform sampler2D s_texture;
607 uniform float alpha; 649 uniform float alpha;
608 uniform vec4 fragmentTexTransform; 650 uniform vec4 fragmentTexClamp;
609 uniform vec3 edge[8]; 651 uniform vec3 edge[8];
610 void main() 652 void main()
611 { 653 {
612 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.z w + fragmentTexTransform.xy; 654 vec2 texCoord = clamp(v_texCoord, fragmentTexClamp.xy, fragmentTexCl amp.zw);
brianderson 2013/01/29 03:03:48 Will this be more efficient?
reveman 2013/01/29 03:40:23 I'm worried it might be significantly less efficie
brianderson 2013/01/29 18:45:12 The _SAT modifier is pretty cool. Clamp would be
613 vec4 texColor = texture2D(s_texture, texCoord); 655 vec4 texColor = texture2D(s_texture, texCoord);
614 vec3 pos = vec3(gl_FragCoord.xy, 1); 656 vec3 pos = vec3(gl_FragCoord.xy, 1);
615 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0); 657 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
616 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0); 658 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
617 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0); 659 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
618 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0); 660 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
619 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0); 661 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
620 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0); 662 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
621 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0); 663 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
622 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0); 664 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
623 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min (a4, a6) * min(a5, a7)); 665 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min (a4, a6) * min(a5, a7));
624 } 666 }
625 ); 667 );
626 } 668 }
627 669
628 std::string FragmentShaderRGBATexClampSwizzleAlphaAA::getShaderString() const 670 std::string FragmentShaderRGBATexClampSwizzleAlphaAA::getShaderString() const
629 { 671 {
630 return SHADER( 672 return SHADER(
631 precision mediump float; 673 precision mediump float;
632 varying vec2 v_texCoord; 674 varying vec2 v_texCoord;
633 uniform sampler2D s_texture; 675 uniform sampler2D s_texture;
634 uniform float alpha; 676 uniform float alpha;
635 uniform vec4 fragmentTexTransform; 677 uniform vec4 fragmentTexClamp;
636 uniform vec3 edge[8]; 678 uniform vec3 edge[8];
637 void main() 679 void main()
638 { 680 {
639 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.z w + fragmentTexTransform.xy; 681 vec2 texCoord = clamp(v_texCoord, fragmentTexClamp.xy, fragmentTexCl amp.zw);
640 vec4 texColor = texture2D(s_texture, texCoord); 682 vec4 texColor = texture2D(s_texture, texCoord);
641 vec3 pos = vec3(gl_FragCoord.xy, 1); 683 vec3 pos = vec3(gl_FragCoord.xy, 1);
642 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0); 684 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
643 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0); 685 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
644 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0); 686 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
645 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0); 687 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
646 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0); 688 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
647 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0); 689 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
648 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0); 690 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
649 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0); 691 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 vec4 color2 = color; 938 vec4 color2 = color;
897 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * texTransform.zw + texT ransform.xy; 939 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * texTransform.zw + texT ransform.xy;
898 vec2 coord = mod(floor(texCoord * frequency * 2.0), 2.0); 940 vec2 coord = mod(floor(texCoord * frequency * 2.0), 2.0);
899 float picker = abs(coord.x - coord.y); 941 float picker = abs(coord.x - coord.y);
900 gl_FragColor = mix(color1, color2, picker) * alpha; 942 gl_FragColor = mix(color1, color2, picker) * alpha;
901 } 943 }
902 ); 944 );
903 } 945 }
904 946
905 } // namespace cc 947 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698