GrGLSLVarying.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/gpu/GrShaderCaps.h"
  8. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  9. #include "src/gpu/glsl/GrGLSLVarying.h"
  10. void GrGLSLVaryingHandler::addPassThroughAttribute(const GrGeometryProcessor::Attribute& input,
  11. const char* output,
  12. Interpolation interpolation) {
  13. SkASSERT(input.isInitialized());
  14. SkASSERT(!fProgramBuilder->primitiveProcessor().willUseGeoShader());
  15. GrGLSLVarying v(input.gpuType());
  16. this->addVarying(input.name(), &v, interpolation);
  17. fProgramBuilder->fVS.codeAppendf("%s = %s;", v.vsOut(), input.name());
  18. fProgramBuilder->fFS.codeAppendf("%s = %s;", output, v.fsIn());
  19. }
  20. static bool use_flat_interpolation(GrGLSLVaryingHandler::Interpolation interpolation,
  21. const GrShaderCaps& shaderCaps) {
  22. switch (interpolation) {
  23. using Interpolation = GrGLSLVaryingHandler::Interpolation;
  24. case Interpolation::kInterpolated:
  25. return false;
  26. case Interpolation::kCanBeFlat:
  27. SkASSERT(!shaderCaps.preferFlatInterpolation() ||
  28. shaderCaps.flatInterpolationSupport());
  29. return shaderCaps.preferFlatInterpolation();
  30. case Interpolation::kMustBeFlat:
  31. SkASSERT(shaderCaps.flatInterpolationSupport());
  32. return true;
  33. }
  34. SK_ABORT("Invalid interpolation");
  35. return false;
  36. }
  37. void GrGLSLVaryingHandler::addVarying(const char* name, GrGLSLVarying* varying,
  38. Interpolation interpolation) {
  39. SkASSERT(GrSLTypeIsFloatType(varying->type()) || Interpolation::kMustBeFlat == interpolation);
  40. bool willUseGeoShader = fProgramBuilder->primitiveProcessor().willUseGeoShader();
  41. VaryingInfo& v = fVaryings.push_back();
  42. SkASSERT(varying);
  43. SkASSERT(kVoid_GrSLType != varying->fType);
  44. v.fType = varying->fType;
  45. v.fIsFlat = use_flat_interpolation(interpolation, *fProgramBuilder->shaderCaps());
  46. fProgramBuilder->nameVariable(&v.fVsOut, 'v', name);
  47. v.fVisibility = kNone_GrShaderFlags;
  48. if (varying->isInVertexShader()) {
  49. varying->fVsOut = v.fVsOut.c_str();
  50. v.fVisibility |= kVertex_GrShaderFlag;
  51. }
  52. if (willUseGeoShader) {
  53. fProgramBuilder->nameVariable(&v.fGsOut, 'g', name);
  54. varying->fGsIn = v.fVsOut.c_str();
  55. varying->fGsOut = v.fGsOut.c_str();
  56. v.fVisibility |= kGeometry_GrShaderFlag;
  57. }
  58. if (varying->isInFragmentShader()) {
  59. varying->fFsIn = (willUseGeoShader ? v.fGsOut : v.fVsOut).c_str();
  60. v.fVisibility |= kFragment_GrShaderFlag;
  61. }
  62. }
  63. void GrGLSLVaryingHandler::emitAttributes(const GrGeometryProcessor& gp) {
  64. for (const auto& attr : gp.vertexAttributes()) {
  65. this->addAttribute(attr.asShaderVar());
  66. }
  67. for (const auto& attr : gp.instanceAttributes()) {
  68. this->addAttribute(attr.asShaderVar());
  69. }
  70. }
  71. void GrGLSLVaryingHandler::addAttribute(const GrShaderVar& var) {
  72. SkASSERT(GrShaderVar::kIn_TypeModifier == var.getTypeModifier());
  73. for (int j = 0; j < fVertexInputs.count(); ++j) {
  74. const GrShaderVar& attr = fVertexInputs[j];
  75. // if attribute already added, don't add it again
  76. if (attr.getName().equals(var.getName())) {
  77. return;
  78. }
  79. }
  80. fVertexInputs.push_back(var);
  81. }
  82. void GrGLSLVaryingHandler::setNoPerspective() {
  83. const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
  84. if (!caps.noperspectiveInterpolationSupport()) {
  85. return;
  86. }
  87. if (const char* extension = caps.noperspectiveInterpolationExtensionString()) {
  88. int bit = 1 << GrGLSLFragmentBuilder::kNoPerspectiveInterpolation_GLSLPrivateFeature;
  89. fProgramBuilder->fVS.addFeature(bit, extension);
  90. if (fProgramBuilder->primitiveProcessor().willUseGeoShader()) {
  91. fProgramBuilder->fGS.addFeature(bit, extension);
  92. }
  93. fProgramBuilder->fFS.addFeature(bit, extension);
  94. }
  95. fDefaultInterpolationModifier = "noperspective";
  96. }
  97. void GrGLSLVaryingHandler::finalize() {
  98. for (int i = 0; i < fVaryings.count(); ++i) {
  99. const VaryingInfo& v = this->fVaryings[i];
  100. const char* modifier = v.fIsFlat ? "flat" : fDefaultInterpolationModifier;
  101. if (v.fVisibility & kVertex_GrShaderFlag) {
  102. fVertexOutputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kOut_TypeModifier,
  103. nullptr, modifier);
  104. if (v.fVisibility & kGeometry_GrShaderFlag) {
  105. fGeomInputs.push_back().set(v.fType, v.fVsOut, GrShaderVar::kUnsizedArray,
  106. GrShaderVar::kIn_TypeModifier, nullptr, modifier);
  107. }
  108. }
  109. if (v.fVisibility & kFragment_GrShaderFlag) {
  110. const char* fsIn = v.fVsOut.c_str();
  111. if (v.fVisibility & kGeometry_GrShaderFlag) {
  112. fGeomOutputs.push_back().set(v.fType, v.fGsOut, GrShaderVar::kOut_TypeModifier,
  113. nullptr, modifier);
  114. fsIn = v.fGsOut.c_str();
  115. }
  116. fFragInputs.push_back().set(v.fType, fsIn, GrShaderVar::kIn_TypeModifier, nullptr,
  117. modifier);
  118. }
  119. }
  120. this->onFinalize();
  121. }
  122. void GrGLSLVaryingHandler::appendDecls(const VarArray& vars, SkString* out) const {
  123. for (int i = 0; i < vars.count(); ++i) {
  124. vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
  125. out->append(";");
  126. }
  127. }
  128. void GrGLSLVaryingHandler::getVertexDecls(SkString* inputDecls, SkString* outputDecls) const {
  129. this->appendDecls(fVertexInputs, inputDecls);
  130. this->appendDecls(fVertexOutputs, outputDecls);
  131. }
  132. void GrGLSLVaryingHandler::getGeomDecls(SkString* inputDecls, SkString* outputDecls) const {
  133. this->appendDecls(fGeomInputs, inputDecls);
  134. this->appendDecls(fGeomOutputs, outputDecls);
  135. }
  136. void GrGLSLVaryingHandler::getFragDecls(SkString* inputDecls, SkString* outputDecls) const {
  137. // We should not have any outputs in the fragment shader when using version 1.10
  138. SkASSERT(k110_GrGLSLGeneration != fProgramBuilder->shaderCaps()->generation() ||
  139. fFragOutputs.empty());
  140. this->appendDecls(fFragInputs, inputDecls);
  141. this->appendDecls(fFragOutputs, outputDecls);
  142. }