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 10 #include "include/core/SkRefCnt.h" 11 #include "include/core/SkSize.h" 12 #include "include/gpu/ganesh/gl/GrGLTypes.h" 13 #include "include/private/base/SkAssert.h" 14 #include "include/private/gpu/ganesh/GrTypesPriv.h" 15 #include "src/gpu/ganesh/GrFragmentProcessor.h" 16 #include "src/gpu/ganesh/GrGeometryProcessor.h" 17 #include "src/gpu/ganesh/GrXferProcessor.h" 18 #include "src/gpu/ganesh/gl/GrGLProgramDataManager.h" 19 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" 20 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" 21 22 #include <cstddef> 23 #include <memory> 24 #include <vector> 25 26 class GrGLGpu; 27 class GrPipeline; 28 class GrProgramInfo; 29 class GrRenderTarget; 30 class GrSurfaceProxy; 31 enum GrSurfaceOrigin : int; 32 enum class SkSLType : char; 33 34 /** 35 * This class manages a GPU program and records per-program information. It also records the vertex 36 * and instance attribute layouts that are to be used with the program. 37 */ 38 class GrGLProgram : public SkRefCnt { 39 public: 40 /** 41 * This class has its own Attribute representation as it does not need the name and we don't 42 * want to worry about copying the name string to memory with life time of GrGLProgram. 43 * Additionally, these store the attribute location. 44 */ 45 struct Attribute { 46 GrVertexAttribType fCPUType; 47 SkSLType fGPUType; 48 size_t fOffset; 49 GrGLint fLocation; 50 }; 51 52 using UniformHandle = GrGLSLProgramDataManager::UniformHandle; 53 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray; 54 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray; 55 56 /** 57 * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with 58 * the vertex attributes preceding the instance attributes. 59 */ 60 static sk_sp<GrGLProgram> Make( 61 GrGLGpu*, 62 const GrGLSLBuiltinUniformHandles&, 63 GrGLuint programID, 64 const UniformInfoArray& uniforms, 65 const UniformInfoArray& textureSamplers, 66 std::unique_ptr<GrGeometryProcessor::ProgramImpl>, 67 std::unique_ptr<GrXferProcessor::ProgramImpl>, 68 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fps, 69 std::unique_ptr<Attribute[]>, 70 int vertexAttributeCnt, 71 int instanceAttributeCnt, 72 int vertexStride, 73 int instanceStride); 74 75 ~GrGLProgram() override; 76 77 /** 78 * Call to abandon GL objects owned by this program. 79 */ 80 void abandon(); 81 82 /** 83 * Gets the GL program ID for this program. 84 */ programID()85 GrGLuint programID() const { return fProgramID; } 86 87 /** 88 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device 89 * space and to make device space positions have the correct origin for processors that require 90 * them. 91 */ 92 struct RenderTargetState { 93 SkISize fRenderTargetSize; 94 GrSurfaceOrigin fRenderTargetOrigin; 95 RenderTargetStateRenderTargetState96 RenderTargetState() { this->invalidate(); } invalidateRenderTargetState97 void invalidate() { 98 fRenderTargetSize.fWidth = -1; 99 fRenderTargetSize.fHeight = -1; 100 fRenderTargetOrigin = (GrSurfaceOrigin) -1; 101 } 102 }; 103 104 /** 105 * This function uploads uniforms and calls each GrGLSL*Processor's setData. 106 * 107 * It is the caller's responsibility to ensure the program is bound before calling. 108 */ 109 void updateUniforms(const GrRenderTarget*, const GrProgramInfo&); 110 111 /** 112 * Binds all geometry processor and fragment processor textures. 113 */ 114 void bindTextures(const GrGeometryProcessor&, 115 const GrSurfaceProxy* const geomProcTextures[], 116 const GrPipeline&); 117 vertexStride()118 int vertexStride() const { return fVertexStride; } instanceStride()119 int instanceStride() const { return fInstanceStride; } 120 numVertexAttributes()121 int numVertexAttributes() const { return fVertexAttributeCnt; } vertexAttribute(int i)122 const Attribute& vertexAttribute(int i) const { 123 SkASSERT(i >= 0 && i < fVertexAttributeCnt); 124 return fAttributes[i]; 125 } 126 numInstanceAttributes()127 int numInstanceAttributes() const { return fInstanceAttributeCnt; } instanceAttribute(int i)128 const Attribute& instanceAttribute(int i) const { 129 SkASSERT(i >= 0 && i < fInstanceAttributeCnt); 130 return fAttributes[i + fVertexAttributeCnt]; 131 } 132 133 private: 134 GrGLProgram(GrGLGpu*, 135 const GrGLSLBuiltinUniformHandles&, 136 GrGLuint programID, 137 const UniformInfoArray& uniforms, 138 const UniformInfoArray& textureSamplers, 139 std::unique_ptr<GrGeometryProcessor::ProgramImpl>, 140 std::unique_ptr<GrXferProcessor::ProgramImpl>, 141 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls, 142 std::unique_ptr<Attribute[]>, 143 int vertexAttributeCnt, 144 int instanceAttributeCnt, 145 int vertexStride, 146 int instanceStride); 147 148 // Helper for setData() that sets the view matrix and loads the render target height uniform 149 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrGeometryProcessor&); 150 151 // these reflect the current values of uniforms (GL uniform values travel with program) 152 RenderTargetState fRenderTargetState; 153 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; 154 GrGLuint fProgramID; 155 156 // the installed effects 157 std::unique_ptr<GrGeometryProcessor::ProgramImpl> fGPImpl; 158 std::unique_ptr<GrXferProcessor::ProgramImpl> fXPImpl; 159 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fFPImpls; 160 161 std::unique_ptr<Attribute[]> fAttributes; 162 int fVertexAttributeCnt; 163 int fInstanceAttributeCnt; 164 int fVertexStride; 165 int fInstanceStride; 166 167 GrGLGpu* fGpu; 168 GrGLProgramDataManager fProgramDataManager; 169 170 int fNumTextureSamplers; 171 172 using INHERITED = SkRefCnt; 173 }; 174 175 #endif 176