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

Side by Side Diff: src/gpu/gl/GrGLProgram.cpp

Issue 12462008: Add GrEllipseEdgeEffect (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: rebase again Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « src/gpu/gl/GrGLEffect.cpp ('k') | src/gpu/gl/GrGLSL.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrGLProgram.h" 8 #include "GrGLProgram.h"
9 9
10 #include "GrAllocator.h" 10 #include "GrAllocator.h"
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 GrGLSLAdd4f(&sum, colorStr.c_str(), constStr.c_str()); 412 GrGLSLAdd4f(&sum, colorStr.c_str(), constStr.c_str());
413 builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str()); 413 builder->fsCodeAppendf("\t%s = %s;\n", outputVar, sum.c_str());
414 } 414 }
415 } 415 }
416 416
417 bool GrGLProgram::genEdgeCoverage(SkString* coverageVar, 417 bool GrGLProgram::genEdgeCoverage(SkString* coverageVar,
418 GrGLShaderBuilder* builder) const { 418 GrGLShaderBuilder* builder) const {
419 if (fDesc.fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) { 419 if (fDesc.fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) {
420 const char *vsName, *fsName; 420 const char *vsName, *fsName;
421 builder->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName); 421 builder->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName);
422 builder->fVSAttrs.push_back().set(kVec4f_GrSLType, 422 builder->addAttribute(kVec4f_GrSLType, EDGE_ATTR_NAME);
423 GrGLShaderVar::kAttribute_TypeModifier ,
424 EDGE_ATTR_NAME);
425 builder->vsCodeAppendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName); 423 builder->vsCodeAppendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
426 switch (fDesc.fVertexEdgeType) { 424 switch (fDesc.fVertexEdgeType) {
427 case GrDrawState::kHairLine_EdgeType: 425 case GrDrawState::kHairLine_EdgeType:
428 builder->fsCodeAppendf("\tfloat edgeAlpha = abs(dot(vec3(%s.xy,1), % s.xyz));\n", builder->fragmentPosition(), fsName); 426 builder->fsCodeAppendf("\tfloat edgeAlpha = abs(dot(vec3(%s.xy,1), % s.xyz));\n", builder->fragmentPosition(), fsName);
429 builder->fsCodeAppendf("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n") ; 427 builder->fsCodeAppendf("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n") ;
430 break; 428 break;
431 case GrDrawState::kQuad_EdgeType: 429 case GrDrawState::kQuad_EdgeType:
432 builder->fsCodeAppendf("\tfloat edgeAlpha;\n"); 430 builder->fsCodeAppendf("\tfloat edgeAlpha;\n");
433 // keep the derivative instructions outside the conditional 431 // keep the derivative instructions outside the conditional
434 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); 432 builder->fsCodeAppendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
(...skipping 25 matching lines...) Expand all
460 builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); 458 builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
461 } 459 }
462 break; 460 break;
463 case GrDrawState::kCircle_EdgeType: 461 case GrDrawState::kCircle_EdgeType:
464 builder->fsCodeAppend("\tfloat edgeAlpha;\n"); 462 builder->fsCodeAppend("\tfloat edgeAlpha;\n");
465 builder->fsCodeAppendf("\tfloat d = distance(%s.xy, %s.xy);\n", buil der->fragmentPosition(), fsName); 463 builder->fsCodeAppendf("\tfloat d = distance(%s.xy, %s.xy);\n", buil der->fragmentPosition(), fsName);
466 builder->fsCodeAppendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); 464 builder->fsCodeAppendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName);
467 builder->fsCodeAppendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smo othstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName); 465 builder->fsCodeAppendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smo othstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName);
468 builder->fsCodeAppend("\tedgeAlpha = outerAlpha * innerAlpha;\n"); 466 builder->fsCodeAppend("\tedgeAlpha = outerAlpha * innerAlpha;\n");
469 break; 467 break;
470 case GrDrawState::kEllipse_EdgeType:
471 builder->fsCodeAppend("\tfloat edgeAlpha;\n");
472 builder->fsCodeAppendf("\tvec2 offset = (%s.xy - %s.xy);\n", builder ->fragmentPosition(), fsName);
473 builder->fsCodeAppendf("\toffset.y *= %s.w;\n", fsName);
474 builder->fsCodeAppend("\tfloat d = length(offset);\n");
475 builder->fsCodeAppendf("\tedgeAlpha = smoothstep(d - 0.5, d + 0.5, % s.z);\n", fsName);
476 break;
477 default: 468 default:
478 GrCrash("Unknown Edge Type!"); 469 GrCrash("Unknown Edge Type!");
479 break; 470 break;
480 } 471 }
481 if (fDesc.fDiscardIfOutsideEdge) { 472 if (fDesc.fDiscardIfOutsideEdge) {
482 builder->fsCodeAppend("\tif (edgeAlpha <= 0.0) {\n\t\tdiscard;\n\t}\ n"); 473 builder->fsCodeAppend("\tif (edgeAlpha <= 0.0) {\n\t\tdiscard;\n\t}\ n");
483 } 474 }
484 *coverageVar = "edgeAlpha"; 475 *coverageVar = "edgeAlpha";
485 return true; 476 return true;
486 } else { 477 } else {
487 coverageVar->reset(); 478 coverageVar->reset();
488 return false; 479 return false;
489 } 480 }
490 } 481 }
491 482
492 void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { 483 void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) {
493 switch (fDesc.fColorInput) { 484 switch (fDesc.fColorInput) {
494 case GrGLProgram::Desc::kAttribute_ColorInput: { 485 case GrGLProgram::Desc::kAttribute_ColorInput: {
495 builder->fVSAttrs.push_back().set(kVec4f_GrSLType, 486 builder->addAttribute(kVec4f_GrSLType, COL_ATTR_NAME);
496 GrGLShaderVar::kAttribute_TypeModifier,
497 COL_ATTR_NAME);
498 const char *vsName, *fsName; 487 const char *vsName, *fsName;
499 builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); 488 builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName);
500 builder->vsCodeAppendf("\t%s = " COL_ATTR_NAME ";\n", vsName); 489 builder->vsCodeAppendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
501 *inColor = fsName; 490 *inColor = fsName;
502 } break; 491 } break;
503 case GrGLProgram::Desc::kUniform_ColorInput: { 492 case GrGLProgram::Desc::kUniform_ColorInput: {
504 const char* name; 493 const char* name;
505 fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::k Fragment_ShaderType, 494 fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::k Fragment_ShaderType,
506 kVec4f_GrSLType, "Co lor", &name); 495 kVec4f_GrSLType, "Co lor", &name);
507 *inColor = name; 496 *inColor = name;
(...skipping 19 matching lines...) Expand all
527 covUniName, inOutCoverage->c_str()); 516 covUniName, inOutCoverage->c_str());
528 *inOutCoverage = "uniCoverage"; 517 *inOutCoverage = "uniCoverage";
529 } else { 518 } else {
530 *inOutCoverage = covUniName; 519 *inOutCoverage = covUniName;
531 } 520 }
532 } 521 }
533 522
534 namespace { 523 namespace {
535 void gen_attribute_coverage(GrGLShaderBuilder* builder, 524 void gen_attribute_coverage(GrGLShaderBuilder* builder,
536 SkString* inOutCoverage) { 525 SkString* inOutCoverage) {
537 builder->fVSAttrs.push_back().set(kVec4f_GrSLType, 526 builder->addAttribute(kVec4f_GrSLType, COV_ATTR_NAME);
538 GrGLShaderVar::kAttribute_TypeModifier,
539 COV_ATTR_NAME);
540 const char *vsName, *fsName; 527 const char *vsName, *fsName;
541 builder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); 528 builder->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName);
542 builder->vsCodeAppendf("\t%s = " COV_ATTR_NAME ";\n", vsName); 529 builder->vsCodeAppendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
543 if (inOutCoverage->size()) { 530 if (inOutCoverage->size()) {
544 builder->fsCodeAppendf("\tvec4 attrCoverage = %s * %s;\n", fsName, inOut Coverage->c_str()); 531 builder->fsCodeAppendf("\tvec4 attrCoverage = %s * %s;\n", fsName, inOut Coverage->c_str());
545 *inOutCoverage = "attrCoverage"; 532 *inOutCoverage = "attrCoverage";
546 } else { 533 } else {
547 *inOutCoverage = fsName; 534 *inOutCoverage = fsName;
548 } 535 }
549 } 536 }
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 this->genInputColor(&builder, &inColor); 757 this->genInputColor(&builder, &inColor);
771 } 758 }
772 759
773 // we output point size in the GS if present 760 // we output point size in the GS if present
774 if (fDesc.fEmitsPointSize && !builder.fUsesGS){ 761 if (fDesc.fEmitsPointSize && !builder.fUsesGS){
775 builder.vsCodeAppend("\tgl_PointSize = 1.0;\n"); 762 builder.vsCodeAppend("\tgl_PointSize = 1.0;\n");
776 } 763 }
777 764
778 // add texture coordinates that are used to the list of vertex attr decls 765 // add texture coordinates that are used to the list of vertex attr decls
779 if (GrDrawState::AttributesBindExplicitTexCoords(attribBindings)) { 766 if (GrDrawState::AttributesBindExplicitTexCoords(attribBindings)) {
780 builder.fVSAttrs.push_back().set(kVec2f_GrSLType, 767 builder.addAttribute(kVec2f_GrSLType, TEX_ATTR_NAME);
781 GrGLShaderVar::kAttribute_TypeModifier,
782 TEX_ATTR_NAME);
783 } 768 }
784 769
785 /////////////////////////////////////////////////////////////////////////// 770 ///////////////////////////////////////////////////////////////////////////
786 // compute the final color 771 // compute the final color
787 772
788 // if we have color stages string them together, feeding the output color 773 // if we have color stages string them together, feeding the output color
789 // of each to the next and generating code for each stage. 774 // of each to the next and generating code for each stage.
790 if (needComputedColor) { 775 if (needComputedColor) {
791 SkString outColor; 776 SkString outColor;
792 for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { 777 for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 *stages[s], 895 *stages[s],
911 fDesc.fEffectKeys[s], 896 fDesc.fEffectKeys[s],
912 inCoverage.size() ? inCovera ge.c_str() : NULL, 897 inCoverage.size() ? inCovera ge.c_str() : NULL,
913 outCoverage.c_str(), 898 outCoverage.c_str(),
914 inCoords, 899 inCoords,
915 &fUniformHandles.fSamplerUni s[s]); 900 &fUniformHandles.fSamplerUni s[s]);
916 builder.setNonStage(); 901 builder.setNonStage();
917 inCoverage = outCoverage; 902 inCoverage = outCoverage;
918 } 903 }
919 } 904 }
905
906 // discard if coverage is zero
907 if (fDesc.fDiscardIfOutsideEdge && !outCoverage.isEmpty()) {
908 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\t\tdiscard;\n\t}\n", outCoverage.c_str());
909 }
920 } 910 }
921 911
922 if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { 912 if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) {
923 builder.fFSOutputs.push_back().set(kVec4f_GrSLType, 913 builder.fFSOutputs.push_back().set(kVec4f_GrSLType,
924 GrGLShaderVar::kOut_TypeModifier, 914 GrGLShaderVar::kOut_TypeModifier,
925 dual_source_output_name()); 915 dual_source_output_name());
926 bool outputIsZero = coverageIsZero; 916 bool outputIsZero = coverageIsZero;
927 SkString coeff; 917 SkString coeff;
928 if (!outputIsZero && 918 if (!outputIsZero &&
929 Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteF ragColorZero) { 919 Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteF ragColorZero) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_ NAME)); 1005 GL_CALL(BindAttribLocation(fProgramID, fDesc.fColorAttributeIndex, COL_ATTR_ NAME));
1016 GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_AT TR_NAME)); 1006 GL_CALL(BindAttribLocation(fProgramID, fDesc.fCoverageAttributeIndex, COV_AT TR_NAME));
1017 1007
1018 if (fDesc.fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) { 1008 if (fDesc.fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) {
1019 GL_CALL(BindAttribLocation(fProgramID, fDesc.fEdgeAttributeIndex, EDGE_A TTR_NAME)); 1009 GL_CALL(BindAttribLocation(fProgramID, fDesc.fEdgeAttributeIndex, EDGE_A TTR_NAME));
1020 } 1010 }
1021 if (GrDrawState::AttributesBindExplicitTexCoords(fDesc.fAttribBindings)) { 1011 if (GrDrawState::AttributesBindExplicitTexCoords(fDesc.fAttribBindings)) {
1022 GL_CALL(BindAttribLocation(fProgramID, fDesc.fTexCoordAttributeIndex, TE X_ATTR_NAME)); 1012 GL_CALL(BindAttribLocation(fProgramID, fDesc.fTexCoordAttributeIndex, TE X_ATTR_NAME));
1023 } 1013 }
1024 1014
1015 const GrGLShaderBuilder::AttributePair* attribEnd = builder.getEffectAttribu tes().end();
1016 for (const GrGLShaderBuilder::AttributePair* attrib = builder.getEffectAttri butes().begin();
1017 attrib != attribEnd;
1018 ++attrib) {
1019 GL_CALL(BindAttribLocation(fProgramID, attrib->fIndex, attrib->fName.c_ str()));
1020 }
1021
1025 GL_CALL(LinkProgram(fProgramID)); 1022 GL_CALL(LinkProgram(fProgramID));
1026 1023
1027 GrGLint linked = GR_GL_INIT_ZERO; 1024 GrGLint linked = GR_GL_INIT_ZERO;
1028 GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); 1025 GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked));
1029 if (!linked) { 1026 if (!linked) {
1030 GrGLint infoLen = GR_GL_INIT_ZERO; 1027 GrGLint infoLen = GR_GL_INIT_ZERO;
1031 GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); 1028 GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen));
1032 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger 1029 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
1033 if (infoLen > 0) { 1030 if (infoLen > 0) {
1034 // retrieve length even though we don't need it to workaround 1031 // retrieve length even though we don't need it to workaround
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
1213 SkScalarToFloat(m[SkMatrix::kMTransX]), 1210 SkScalarToFloat(m[SkMatrix::kMTransX]),
1214 SkScalarToFloat(m[SkMatrix::kMTransY]), 1211 SkScalarToFloat(m[SkMatrix::kMTransY]),
1215 SkScalarToFloat(m[SkMatrix::kMPersp2]) 1212 SkScalarToFloat(m[SkMatrix::kMPersp2])
1216 }; 1213 };
1217 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); 1214 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt);
1218 fMatrixState.fViewMatrix = drawState.getViewMatrix(); 1215 fMatrixState.fViewMatrix = drawState.getViewMatrix();
1219 fMatrixState.fRenderTargetSize = size; 1216 fMatrixState.fRenderTargetSize = size;
1220 fMatrixState.fRenderTargetOrigin = rt->origin(); 1217 fMatrixState.fRenderTargetOrigin = rt->origin();
1221 } 1218 }
1222 } 1219 }
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLEffect.cpp ('k') | src/gpu/gl/GrGLSL.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698