GrGLSLGeometryProcessor.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2014 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/glsl/GrGLSLGeometryProcessor.h"
  8. #include "src/gpu/GrCoordTransform.h"
  9. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  10. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  11. #include "src/gpu/glsl/GrGLSLVarying.h"
  12. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  13. void GrGLSLGeometryProcessor::emitCode(EmitArgs& args) {
  14. GrGPArgs gpArgs;
  15. this->onEmitCode(args, &gpArgs);
  16. GrGLSLVertexBuilder* vBuilder = args.fVertBuilder;
  17. if (!args.fGP.willUseGeoShader()) {
  18. // Emit the vertex position to the hardware in the normalized window coordinates it expects.
  19. SkASSERT(kFloat2_GrSLType == gpArgs.fPositionVar.getType() ||
  20. kFloat3_GrSLType == gpArgs.fPositionVar.getType());
  21. vBuilder->emitNormalizedSkPosition(gpArgs.fPositionVar.c_str(), args.fRTAdjustName,
  22. gpArgs.fPositionVar.getType());
  23. if (kFloat2_GrSLType == gpArgs.fPositionVar.getType()) {
  24. args.fVaryingHandler->setNoPerspective();
  25. }
  26. } else {
  27. // Since we have a geometry shader, leave the vertex position in Skia device space for now.
  28. // The geometry Shader will operate in device space, and then convert the final positions to
  29. // normalized hardware window coordinates under the hood, once everything else has finished.
  30. // The subclass must call setNoPerspective on the varying handler, if applicable.
  31. vBuilder->codeAppendf("sk_Position = float4(%s", gpArgs.fPositionVar.c_str());
  32. switch (gpArgs.fPositionVar.getType()) {
  33. case kFloat_GrSLType:
  34. vBuilder->codeAppend(", 0"); // fallthru.
  35. case kFloat2_GrSLType:
  36. vBuilder->codeAppend(", 0"); // fallthru.
  37. case kFloat3_GrSLType:
  38. vBuilder->codeAppend(", 1"); // fallthru.
  39. case kFloat4_GrSLType:
  40. vBuilder->codeAppend(");");
  41. break;
  42. default:
  43. SK_ABORT("Invalid position var type");
  44. break;
  45. }
  46. }
  47. }
  48. void GrGLSLGeometryProcessor::emitTransforms(GrGLSLVertexBuilder* vb,
  49. GrGLSLVaryingHandler* varyingHandler,
  50. GrGLSLUniformHandler* uniformHandler,
  51. const GrShaderVar& localCoordsVar,
  52. const SkMatrix& localMatrix,
  53. FPCoordTransformHandler* handler) {
  54. SkASSERT(GrSLTypeIsFloatType(localCoordsVar.getType()));
  55. SkASSERT(2 == GrSLTypeVecLength(localCoordsVar.getType()) ||
  56. 3 == GrSLTypeVecLength(localCoordsVar.getType()));
  57. bool threeComponentLocalCoords = 3 == GrSLTypeVecLength(localCoordsVar.getType());
  58. SkString localCoords;
  59. if (threeComponentLocalCoords) {
  60. localCoords = localCoordsVar.getName();
  61. } else {
  62. localCoords.printf("float3(%s, 1)", localCoordsVar.c_str());
  63. }
  64. int i = 0;
  65. while (const GrCoordTransform* coordTransform = handler->nextCoordTransform()) {
  66. SkString strUniName;
  67. strUniName.printf("CoordTransformMatrix_%d", i);
  68. const char* uniName;
  69. fInstalledTransforms.push_back().fHandle = uniformHandler->addUniform(kVertex_GrShaderFlag,
  70. kFloat3x3_GrSLType,
  71. strUniName.c_str(),
  72. &uniName).toIndex();
  73. GrSLType varyingType = kFloat2_GrSLType;
  74. if (localMatrix.hasPerspective() || coordTransform->getMatrix().hasPerspective()
  75. || threeComponentLocalCoords) {
  76. varyingType = kFloat3_GrSLType;
  77. }
  78. SkString strVaryingName;
  79. strVaryingName.printf("TransformedCoords_%d", i);
  80. GrGLSLVarying v(varyingType);
  81. varyingHandler->addVarying(strVaryingName.c_str(), &v);
  82. handler->specifyCoordsForCurrCoordTransform(SkString(v.fsIn()), varyingType);
  83. if (kFloat2_GrSLType == varyingType) {
  84. vb->codeAppendf("%s = (%s * %s).xy;", v.vsOut(), uniName, localCoords.c_str());
  85. } else {
  86. vb->codeAppendf("%s = %s * %s;", v.vsOut(), uniName, localCoords.c_str());
  87. }
  88. ++i;
  89. }
  90. }
  91. void GrGLSLGeometryProcessor::setTransformDataHelper(const SkMatrix& localMatrix,
  92. const GrGLSLProgramDataManager& pdman,
  93. FPCoordTransformIter* transformIter) {
  94. int i = 0;
  95. while (const GrCoordTransform* coordTransform = transformIter->next()) {
  96. const SkMatrix& m = GetTransformMatrix(localMatrix, *coordTransform);
  97. if (!fInstalledTransforms[i].fCurrentValue.cheapEqualTo(m)) {
  98. pdman.setSkMatrix(fInstalledTransforms[i].fHandle.toIndex(), m);
  99. fInstalledTransforms[i].fCurrentValue = m;
  100. }
  101. ++i;
  102. }
  103. SkASSERT(i == fInstalledTransforms.count());
  104. }
  105. void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
  106. GrGPArgs* gpArgs,
  107. const char* posName) {
  108. gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
  109. vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName);
  110. }
  111. void GrGLSLGeometryProcessor::writeOutputPosition(GrGLSLVertexBuilder* vertBuilder,
  112. GrGLSLUniformHandler* uniformHandler,
  113. GrGPArgs* gpArgs,
  114. const char* posName,
  115. const SkMatrix& mat,
  116. UniformHandle* viewMatrixUniform) {
  117. if (mat.isIdentity()) {
  118. gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
  119. vertBuilder->codeAppendf("float2 %s = %s;", gpArgs->fPositionVar.c_str(), posName);
  120. } else {
  121. const char* viewMatrixName;
  122. *viewMatrixUniform = uniformHandler->addUniform(kVertex_GrShaderFlag,
  123. kFloat3x3_GrSLType,
  124. "uViewM",
  125. &viewMatrixName);
  126. if (!mat.hasPerspective()) {
  127. gpArgs->fPositionVar.set(kFloat2_GrSLType, "pos2");
  128. vertBuilder->codeAppendf("float2 %s = (%s * float3(%s, 1)).xy;",
  129. gpArgs->fPositionVar.c_str(), viewMatrixName, posName);
  130. } else {
  131. gpArgs->fPositionVar.set(kFloat3_GrSLType, "pos3");
  132. vertBuilder->codeAppendf("float3 %s = %s * float3(%s, 1);",
  133. gpArgs->fPositionVar.c_str(), viewMatrixName, posName);
  134. }
  135. }
  136. }