GrGLSLGeometryProcessor.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2013 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. #ifndef GrGLSLGeometryProcessor_DEFINED
  8. #define GrGLSLGeometryProcessor_DEFINED
  9. #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
  10. class GrGLSLGPBuilder;
  11. /**
  12. * If a GL effect needs a GrGLFullShaderBuilder* object to emit vertex code, then it must inherit
  13. * from this class. Since paths don't have vertices, this class is only meant to be used internally
  14. * by skia, for special cases.
  15. */
  16. class GrGLSLGeometryProcessor : public GrGLSLPrimitiveProcessor {
  17. public:
  18. /* Any general emit code goes in the base class emitCode. Subclasses override onEmitCode */
  19. void emitCode(EmitArgs&) final;
  20. protected:
  21. // A helper which subclasses can use to upload coord transform matrices in setData().
  22. void setTransformDataHelper(const SkMatrix& localMatrix,
  23. const GrGLSLProgramDataManager& pdman,
  24. FPCoordTransformIter*);
  25. // Emit transformed local coords from the vertex shader as a uniform matrix and varying per
  26. // coord-transform. localCoordsVar must be a 2- or 3-component vector. If it is 3 then it is
  27. // assumed to be a 2D homogeneous coordinate.
  28. void emitTransforms(GrGLSLVertexBuilder*,
  29. GrGLSLVaryingHandler*,
  30. GrGLSLUniformHandler*,
  31. const GrShaderVar& localCoordsVar,
  32. const SkMatrix& localMatrix,
  33. FPCoordTransformHandler*);
  34. // Version of above that assumes identity for the local matrix.
  35. void emitTransforms(GrGLSLVertexBuilder* vb,
  36. GrGLSLVaryingHandler* varyingHandler,
  37. GrGLSLUniformHandler* uniformHandler,
  38. const GrShaderVar& localCoordsVar,
  39. FPCoordTransformHandler* handler) {
  40. this->emitTransforms(vb, varyingHandler, uniformHandler, localCoordsVar, SkMatrix::I(),
  41. handler);
  42. }
  43. struct GrGPArgs {
  44. // Used to specify the output variable used by the GP to store its device position. It can
  45. // either be a float2 or a float3 (in order to handle perspective). The subclass sets this
  46. // in its onEmitCode().
  47. GrShaderVar fPositionVar;
  48. };
  49. // Helpers for adding code to write the transformed vertex position. The first simple version
  50. // just writes a variable named by 'posName' into the position output variable with the
  51. // assumption that the position is 2D. The second version transforms the input position by a
  52. // view matrix and the output variable is 2D or 3D depending on whether the view matrix is
  53. // perspective. Both versions declare the output position variable and will set
  54. // GrGPArgs::fPositionVar.
  55. void writeOutputPosition(GrGLSLVertexBuilder*, GrGPArgs*, const char* posName);
  56. void writeOutputPosition(GrGLSLVertexBuilder*,
  57. GrGLSLUniformHandler* uniformHandler,
  58. GrGPArgs*,
  59. const char* posName,
  60. const SkMatrix& mat,
  61. UniformHandle* viewMatrixUniform);
  62. static uint32_t ComputePosKey(const SkMatrix& mat) {
  63. if (mat.isIdentity()) {
  64. return 0x0;
  65. } else if (!mat.hasPerspective()) {
  66. return 0x01;
  67. } else {
  68. return 0x02;
  69. }
  70. }
  71. private:
  72. virtual void onEmitCode(EmitArgs&, GrGPArgs*) = 0;
  73. struct TransformUniform {
  74. UniformHandle fHandle;
  75. SkMatrix fCurrentValue = SkMatrix::InvalidMatrix();
  76. };
  77. SkTArray<TransformUniform, true> fInstalledTransforms;
  78. typedef GrGLSLPrimitiveProcessor INHERITED;
  79. };
  80. #endif