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
20class GrFragmentProcessor;
21class GrGLContextInfo;
22class GrProgramDesc;
23class GrGLSLShaderBuilder;
24class GrShaderCaps;
25
26struct GrGLPrecompiledProgram {
27 GrGLPrecompiledProgram(GrGLuint programID = 0,
28 SkSL::Program::Inputs inputs = SkSL::Program::Inputs())
29 : fProgramID(programID)
30 , fInputs(inputs) {}
31
32 GrGLuint fProgramID;
33 SkSL::Program::Inputs fInputs;
34};
35
36class GrGLProgramBuilder : public GrGLSLProgramBuilder {
37public:
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(GrGLGpu*,
48 GrRenderTarget*,
49 const GrProgramDesc&,
50 const GrProgramInfo&,
51 const GrGLPrecompiledProgram* = nullptr);
52
53 static bool PrecompileProgram(GrGLPrecompiledProgram*, GrGLGpu*, const SkData&);
54
55 const GrCaps* caps() const override;
56
57 GrGLGpu* gpu() const { return fGpu; }
58
59private:
60 GrGLProgramBuilder(GrGLGpu*, GrRenderTarget*, const GrProgramDesc&, const GrProgramInfo&);
61
62 void addInputVars(const SkSL::Program::Inputs& inputs);
63 bool compileAndAttachShaders(const SkSL::String& glsl,
64 GrGLuint programId,
65 GrGLenum type,
66 SkTDArray<GrGLuint>* shaderIds,
67 GrContextOptions::ShaderErrorHandler* errorHandler);
68
69 void computeCountsAndStrides(GrGLuint programID, const GrPrimitiveProcessor& primProc,
70 bool bindAttribLocations);
71 void storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
72 const SkSL::String shaders[], bool isSkSL,
73 SkSL::Program::Settings* settings);
74 sk_sp<GrGLProgram> finalize(const GrGLPrecompiledProgram*);
75 void bindProgramResourceLocations(GrGLuint programID);
76 bool checkLinkStatus(GrGLuint programID, GrContextOptions::ShaderErrorHandler* errorHandler,
77 SkSL::String* sksl[], const SkSL::String glsl[]);
78 void resolveProgramResourceLocations(GrGLuint programID, bool force);
79
80 // Subclasses create different programs
81 sk_sp<GrGLProgram> createProgram(GrGLuint programID);
82
83 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
84 const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
85 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
86
87 GrGLGpu* fGpu;
88 GrGLVaryingHandler fVaryingHandler;
89 GrGLUniformHandler fUniformHandler;
90
91 std::unique_ptr<GrGLProgram::Attribute[]> fAttributes;
92 int fVertexAttributeCnt;
93 int fInstanceAttributeCnt;
94 size_t fVertexStride;
95 size_t fInstanceStride;
96
97 // shader pulled from cache. Data is organized as:
98 // SkSL::Program::Inputs inputs
99 // int binaryFormat
100 // (all remaining bytes) char[] binary
101 sk_sp<SkData> fCached;
102
103 typedef GrGLSLProgramBuilder INHERITED;
104};
105#endif
106