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