GrGLProgram.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "src/gpu/GrAllocator.h"
  8. #include "src/gpu/GrCoordTransform.h"
  9. #include "src/gpu/GrPathProcessor.h"
  10. #include "src/gpu/GrPipeline.h"
  11. #include "src/gpu/GrProcessor.h"
  12. #include "src/gpu/GrTexturePriv.h"
  13. #include "src/gpu/GrXferProcessor.h"
  14. #include "src/gpu/gl/GrGLBuffer.h"
  15. #include "src/gpu/gl/GrGLGpu.h"
  16. #include "src/gpu/gl/GrGLPathRendering.h"
  17. #include "src/gpu/gl/GrGLProgram.h"
  18. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  19. #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
  20. #include "src/gpu/glsl/GrGLSLXferProcessor.h"
  21. #define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X)
  22. #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fGpu->glInterface(), R, X)
  23. ///////////////////////////////////////////////////////////////////////////////////////////////////
  24. GrGLProgram::GrGLProgram(
  25. GrGLGpu* gpu,
  26. const GrGLSLBuiltinUniformHandles& builtinUniforms,
  27. GrGLuint programID,
  28. const UniformInfoArray& uniforms,
  29. const UniformInfoArray& textureSamplers,
  30. const VaryingInfoArray& pathProcVaryings,
  31. std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
  32. std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
  33. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
  34. int fragmentProcessorCnt,
  35. std::unique_ptr<Attribute[]> attributes,
  36. int vertexAttributeCnt,
  37. int instanceAttributeCnt,
  38. int vertexStride,
  39. int instanceStride)
  40. : fBuiltinUniformHandles(builtinUniforms)
  41. , fProgramID(programID)
  42. , fPrimitiveProcessor(std::move(geometryProcessor))
  43. , fXferProcessor(std::move(xferProcessor))
  44. , fFragmentProcessors(std::move(fragmentProcessors))
  45. , fFragmentProcessorCnt(fragmentProcessorCnt)
  46. , fAttributes(std::move(attributes))
  47. , fVertexAttributeCnt(vertexAttributeCnt)
  48. , fInstanceAttributeCnt(instanceAttributeCnt)
  49. , fVertexStride(vertexStride)
  50. , fInstanceStride(instanceStride)
  51. , fGpu(gpu)
  52. , fProgramDataManager(gpu, programID, uniforms, pathProcVaryings)
  53. , fNumTextureSamplers(textureSamplers.count()) {
  54. // Assign texture units to sampler uniforms one time up front.
  55. GL_CALL(UseProgram(fProgramID));
  56. fProgramDataManager.setSamplerUniforms(textureSamplers, 0);
  57. }
  58. GrGLProgram::~GrGLProgram() {
  59. if (fProgramID) {
  60. GL_CALL(DeleteProgram(fProgramID));
  61. }
  62. }
  63. void GrGLProgram::abandon() {
  64. fProgramID = 0;
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////
  67. void GrGLProgram::updateUniformsAndTextureBindings(const GrRenderTarget* renderTarget,
  68. GrSurfaceOrigin origin,
  69. const GrPrimitiveProcessor& primProc,
  70. const GrPipeline& pipeline,
  71. const GrTextureProxy* const primProcTextures[]) {
  72. this->setRenderTargetState(renderTarget, origin, primProc);
  73. // we set the textures, and uniforms for installed processors in a generic way, but subclasses
  74. // of GLProgram determine how to set coord transforms
  75. // We must bind to texture units in the same order in which we set the uniforms in
  76. // GrGLProgramDataManager. That is, we bind textures for processors in this order:
  77. // primProc, fragProcs, XP.
  78. fPrimitiveProcessor->setData(fProgramDataManager, primProc,
  79. GrFragmentProcessor::CoordTransformIter(pipeline));
  80. if (primProcTextures) {
  81. this->updatePrimitiveProcessorTextureBindings(primProc, primProcTextures);
  82. }
  83. int nextTexSamplerIdx = primProc.numTextureSamplers();
  84. this->setFragmentData(pipeline, &nextTexSamplerIdx);
  85. const GrXferProcessor& xp = pipeline.getXferProcessor();
  86. SkIPoint offset;
  87. GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
  88. fXferProcessor->setData(fProgramDataManager, xp, dstTexture, offset);
  89. if (dstTexture) {
  90. fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::ClampNearest(),
  91. pipeline.dstTextureProxy()->textureSwizzle(),
  92. static_cast<GrGLTexture*>(dstTexture));
  93. }
  94. SkASSERT(nextTexSamplerIdx == fNumTextureSamplers);
  95. }
  96. void GrGLProgram::updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor& primProc,
  97. const GrTextureProxy* const proxies[]) {
  98. for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
  99. auto* tex = static_cast<GrGLTexture*>(proxies[i]->peekTexture());
  100. fGpu->bindTexture(i, primProc.textureSampler(i).samplerState(),
  101. primProc.textureSampler(i).swizzle(), tex);
  102. }
  103. }
  104. void GrGLProgram::setFragmentData(const GrPipeline& pipeline, int* nextTexSamplerIdx) {
  105. GrFragmentProcessor::Iter iter(pipeline);
  106. GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
  107. const GrFragmentProcessor* fp = iter.next();
  108. GrGLSLFragmentProcessor* glslFP = glslIter.next();
  109. while (fp && glslFP) {
  110. glslFP->setData(fProgramDataManager, *fp);
  111. for (int i = 0; i < fp->numTextureSamplers(); ++i) {
  112. const GrFragmentProcessor::TextureSampler& sampler = fp->textureSampler(i);
  113. fGpu->bindTexture((*nextTexSamplerIdx)++, sampler.samplerState(), sampler.swizzle(),
  114. static_cast<GrGLTexture*>(sampler.peekTexture()));
  115. }
  116. fp = iter.next();
  117. glslFP = glslIter.next();
  118. }
  119. SkASSERT(!fp && !glslFP);
  120. }
  121. void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin,
  122. const GrPrimitiveProcessor& primProc) {
  123. // Load the RT size uniforms if they are needed
  124. if (fBuiltinUniformHandles.fRTWidthUni.isValid() &&
  125. fRenderTargetState.fRenderTargetSize.fWidth != rt->width()) {
  126. fProgramDataManager.set1f(fBuiltinUniformHandles.fRTWidthUni, SkIntToScalar(rt->width()));
  127. }
  128. if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
  129. fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
  130. fProgramDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
  131. }
  132. // set RT adjustment
  133. SkISize size;
  134. size.set(rt->width(), rt->height());
  135. if (!primProc.isPathRendering()) {
  136. if (fRenderTargetState.fRenderTargetOrigin != origin ||
  137. fRenderTargetState.fRenderTargetSize != size) {
  138. fRenderTargetState.fRenderTargetSize = size;
  139. fRenderTargetState.fRenderTargetOrigin = origin;
  140. float rtAdjustmentVec[4];
  141. fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
  142. fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
  143. }
  144. } else {
  145. SkASSERT(fGpu->glCaps().shaderCaps()->pathRenderingSupport());
  146. const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
  147. fGpu->glPathRendering()->setProjectionMatrix(pathProc.viewMatrix(),
  148. size, origin);
  149. }
  150. }