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 8 #ifndef GrGLProgramBuilder_DEFINED 9 #define GrGLProgramBuilder_DEFINED 10 11 #include "include/core/SkData.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkString.h" 14 #include "include/gpu/ganesh/GrContextOptions.h" 15 #include "include/gpu/ganesh/gl/GrGLTypes.h" 16 #include "include/private/base/SkTDArray.h" // IWYU pragma: keep 17 #include "src/gpu/ganesh/GrGeometryProcessor.h" 18 #include "src/gpu/ganesh/gl/GrGLProgram.h" 19 #include "src/gpu/ganesh/gl/GrGLUniformHandler.h" 20 #include "src/gpu/ganesh/gl/GrGLVaryingHandler.h" 21 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h" 22 #include "src/sksl/ir/SkSLProgram.h" 23 24 #include <cstddef> 25 #include <memory> 26 #include <string> 27 28 class GrCaps; 29 class GrDirectContext; 30 class GrGLGpu; 31 class GrGLSLUniformHandler; 32 class GrGLSLVaryingHandler; 33 class GrProgramDesc; 34 class GrProgramInfo; 35 36 namespace SkSL { 37 struct ProgramSettings; 38 } 39 40 struct GrGLPrecompiledProgram { 41 GrGLPrecompiledProgram(GrGLuint programID = 0, 42 SkSL::Program::Interface intf = SkSL::Program::Interface()) fProgramIDGrGLPrecompiledProgram43 : fProgramID(programID), fInterface(intf) {} 44 45 GrGLuint fProgramID; 46 SkSL::Program::Interface fInterface; 47 }; 48 49 class GrGLProgramBuilder : public GrGLSLProgramBuilder { 50 public: 51 /** Generates a shader program. 52 * 53 * The program implements what is specified in the stages given as input. 54 * After successful generation, the builder result objects are available 55 * to be used. 56 * If a GL program has already been created, the program ID and interface can 57 * be supplied to skip the shader compilation. 58 * @return the created program if generation was successful. 59 */ 60 static sk_sp<GrGLProgram> CreateProgram(GrDirectContext*, 61 const GrProgramDesc&, 62 const GrProgramInfo&, 63 const GrGLPrecompiledProgram* = nullptr); 64 65 static bool PrecompileProgram(GrDirectContext*, GrGLPrecompiledProgram*, const SkData&); 66 67 const GrCaps* caps() const override; 68 gpu()69 GrGLGpu* gpu() const { return fGpu; } 70 71 private: 72 GrGLProgramBuilder(GrGLGpu*, const GrProgramDesc&, const GrProgramInfo&); 73 74 void addInputVars(const SkSL::Program::Interface&); 75 bool compileAndAttachShaders(const std::string& glsl, 76 GrGLuint programId, 77 GrGLenum type, 78 SkTDArray<GrGLuint>* shaderIds, 79 bool shaderWasCached, 80 GrContextOptions::ShaderErrorHandler* errorHandler); 81 82 void computeCountsAndStrides(GrGLuint programID, 83 const GrGeometryProcessor&, 84 bool bindAttribLocations); 85 void storeShaderInCache(const SkSL::Program::Interface&, 86 GrGLuint programID, 87 const std::string shaders[], 88 bool isSkSL, 89 SkSL::ProgramSettings* settings); 90 sk_sp<GrGLProgram> finalize(const GrGLPrecompiledProgram*); 91 void bindProgramResourceLocations(GrGLuint programID); 92 void resolveProgramResourceLocations(GrGLuint programID, bool force); 93 94 // Subclasses create different programs 95 sk_sp<GrGLProgram> createProgram(GrGLuint programID); 96 uniformHandler()97 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } uniformHandler()98 const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; } varyingHandler()99 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; } 100 101 GrGLGpu* fGpu; 102 GrGLVaryingHandler fVaryingHandler; 103 GrGLUniformHandler fUniformHandler; 104 105 std::unique_ptr<GrGLProgram::Attribute[]> fAttributes; 106 int fVertexAttributeCnt; 107 int fInstanceAttributeCnt; 108 size_t fVertexStride; 109 size_t fInstanceStride; 110 111 // shader pulled from cache. Data is organized as: 112 // SkSL::Program::Interface interface 113 // int binaryFormat 114 // (all remaining bytes) char[] binary 115 sk_sp<SkData> fCached; 116 117 using INHERITED = GrGLSLProgramBuilder; 118 }; 119 #endif 120