GrGLProgram.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2011 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 GrGLProgram_DEFINED
  8. #define GrGLProgram_DEFINED
  9. #include "src/gpu/gl/GrGLProgramDataManager.h"
  10. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  11. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  12. class GrGLSLFragmentProcessor;
  13. class GrGLSLPrimitiveProcessor;
  14. class GrGLSLXferProcessor;
  15. class GrPipeline;
  16. class GrPrimitiveProcessor;
  17. class GrRenderTarget;
  18. class GrTextureProxy;
  19. /**
  20. * This class manages a GPU program and records per-program information. It also records the vertex
  21. * and instance attribute layouts that are to be used with the program.
  22. */
  23. class GrGLProgram : public SkRefCnt {
  24. public:
  25. /**
  26. * This class has its own Attribute representation as it does not need the name and we don't
  27. * want to worry about copying the name string to memory with life time of GrGLProgram.
  28. * Additionally, these store the attribute location.
  29. */
  30. struct Attribute {
  31. GrVertexAttribType fCPUType;
  32. GrSLType fGPUType;
  33. size_t fOffset;
  34. GrGLint fLocation;
  35. };
  36. using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
  37. using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
  38. using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
  39. /**
  40. * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with
  41. * the vertex attributes preceding the instance attributes.
  42. */
  43. GrGLProgram(GrGLGpu*,
  44. const GrGLSLBuiltinUniformHandles&,
  45. GrGLuint programID,
  46. const UniformInfoArray& uniforms,
  47. const UniformInfoArray& textureSamplers,
  48. const VaryingInfoArray&, // used for NVPR only currently
  49. std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
  50. std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
  51. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
  52. int fragmentProcessorCnt,
  53. std::unique_ptr<Attribute[]>,
  54. int vertexAttributeCnt,
  55. int instanceAttributeCnt,
  56. int vertexStride,
  57. int instanceStride);
  58. ~GrGLProgram();
  59. /**
  60. * Call to abandon GL objects owned by this program.
  61. */
  62. void abandon();
  63. /**
  64. * Gets the GL program ID for this program.
  65. */
  66. GrGLuint programID() const { return fProgramID; }
  67. /**
  68. * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
  69. * space and to make device space positions have the correct origin for processors that require
  70. * them.
  71. */
  72. struct RenderTargetState {
  73. SkISize fRenderTargetSize;
  74. GrSurfaceOrigin fRenderTargetOrigin;
  75. RenderTargetState() { this->invalidate(); }
  76. void invalidate() {
  77. fRenderTargetSize.fWidth = -1;
  78. fRenderTargetSize.fHeight = -1;
  79. fRenderTargetOrigin = (GrSurfaceOrigin) -1;
  80. }
  81. /**
  82. * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
  83. * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
  84. * applied as such:
  85. * pos.x = dot(v.xy, pos.xz)
  86. * pos.y = dot(v.zw, pos.yz)
  87. */
  88. void getRTAdjustmentVec(float* destVec) {
  89. destVec[0] = 2.f / fRenderTargetSize.fWidth;
  90. destVec[1] = -1.f;
  91. if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
  92. destVec[2] = -2.f / fRenderTargetSize.fHeight;
  93. destVec[3] = 1.f;
  94. } else {
  95. destVec[2] = 2.f / fRenderTargetSize.fHeight;
  96. destVec[3] = -1.f;
  97. }
  98. }
  99. };
  100. /**
  101. * This function uploads uniforms, calls each GrGLSL*Processor's setData. It binds all fragment
  102. * processor textures. Primitive process textures can be bound using this function or by
  103. * calling updatePrimitiveProcessorTextureBindings.
  104. *
  105. * It is the caller's responsibility to ensure the program is bound before calling.
  106. */
  107. void updateUniformsAndTextureBindings(const GrRenderTarget*, GrSurfaceOrigin,
  108. const GrPrimitiveProcessor&, const GrPipeline&,
  109. const GrTextureProxy* const primitiveProcessorTextures[]);
  110. void updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor&,
  111. const GrTextureProxy* const[]);
  112. int vertexStride() const { return fVertexStride; }
  113. int instanceStride() const { return fInstanceStride; }
  114. int numVertexAttributes() const { return fVertexAttributeCnt; }
  115. const Attribute& vertexAttribute(int i) const {
  116. SkASSERT(i >= 0 && i < fVertexAttributeCnt);
  117. return fAttributes[i];
  118. }
  119. int numInstanceAttributes() const { return fInstanceAttributeCnt; }
  120. const Attribute& instanceAttribute(int i) const {
  121. SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
  122. return fAttributes[i + fVertexAttributeCnt];
  123. }
  124. private:
  125. // A helper to loop over effects, set the transforms (via subclass) and bind textures
  126. void setFragmentData(const GrPipeline&, int* nextTexSamplerIdx);
  127. // Helper for setData() that sets the view matrix and loads the render target height uniform
  128. void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrPrimitiveProcessor&);
  129. // these reflect the current values of uniforms (GL uniform values travel with program)
  130. RenderTargetState fRenderTargetState;
  131. GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
  132. GrGLuint fProgramID;
  133. // the installed effects
  134. std::unique_ptr<GrGLSLPrimitiveProcessor> fPrimitiveProcessor;
  135. std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
  136. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
  137. int fFragmentProcessorCnt;
  138. std::unique_ptr<Attribute[]> fAttributes;
  139. int fVertexAttributeCnt;
  140. int fInstanceAttributeCnt;
  141. int fVertexStride;
  142. int fInstanceStride;
  143. GrGLGpu* fGpu;
  144. GrGLProgramDataManager fProgramDataManager;
  145. int fNumTextureSamplers;
  146. typedef SkRefCnt INHERITED;
  147. };
  148. #endif