blob: fda1e6583d0baf7301a19b11e0a741bce7aba32a [file] [log] [blame]
Timothy Liang7ac582e2018-08-06 09:47:23 -04001/*
2 * Copyright 2018 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 GrMtlPipelineStateBuilder_DEFINED
9#define GrMtlPipelineStateBuilder_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrPipeline.h"
12#include "src/gpu/GrProgramDesc.h"
13#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14#include "src/gpu/mtl/GrMtlUniformHandler.h"
15#include "src/gpu/mtl/GrMtlVaryingHandler.h"
16#include "src/sksl/SkSLCompiler.h"
Timothy Liang7ac582e2018-08-06 09:47:23 -040017
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020018#import <Metal/Metal.h>
Timothy Liang7ac582e2018-08-06 09:47:23 -040019
Robert Phillips901aff02019-10-08 12:32:56 -040020class GrProgramInfo;
Timothy Liang7ac582e2018-08-06 09:47:23 -040021class GrMtlGpu;
22class GrMtlPipelineState;
23
24class GrMtlPipelineStateBuilder : public GrGLSLProgramBuilder {
25public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050026 /**
27 * For Metal we want to cache the entire pipeline for reuse of draws. The Desc here holds all
28 * the information needed to differentiate one pipeline from another.
29 *
30 * The GrProgramDesc contains all the information need to create the actual shaders for the
31 * pipeline.
32 *
33 * For Metal we need to add to the GrProgramDesc to include the rest of the state on the
34 * pipeline. This includes blending information and primitive type. The pipeline is immutable
35 * so any remaining dynamic state is set via the MtlRenderCmdEncoder.
36 */
37 class Desc : public GrProgramDesc {
38 public:
Robert Phillips901aff02019-10-08 12:32:56 -040039 static bool Build(Desc*, GrRenderTarget*,
40 const GrProgramInfo&, GrPrimitiveType, GrMtlGpu* gpu);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050041
42 size_t shaderKeyLength() const { return fShaderKeyLength; }
43
44 private:
45 size_t fShaderKeyLength;
46
47 typedef GrProgramDesc INHERITED;
48 };
49
50 /** Generates a pipeline state.
51 *
52 * The GrMtlPipelineState implements what is specified in the GrPipeline and
53 * GrPrimitiveProcessor as input. After successful generation, the builder result objects are
54 * available to be used. This function may modify the program key by setting the surface origin
55 * key to 0 (unspecified) if it turns out the program does not care about the surface origin.
56 * @return true if generation was successful.
57 */
58 static GrMtlPipelineState* CreatePipelineState(GrMtlGpu*,
Robert Phillips901aff02019-10-08 12:32:56 -040059 GrRenderTarget*,
60 const GrProgramInfo&,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050061 Desc*);
Timothy Liang7ac582e2018-08-06 09:47:23 -040062
63private:
Robert Phillips901aff02019-10-08 12:32:56 -040064 GrMtlPipelineStateBuilder(GrMtlGpu*, GrRenderTarget*, const GrProgramInfo&, GrProgramDesc*);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050065
Robert Phillips901aff02019-10-08 12:32:56 -040066 GrMtlPipelineState* finalize(GrRenderTarget*, const GrProgramInfo&, Desc*);
Timothy Liang7ac582e2018-08-06 09:47:23 -040067
68 const GrCaps* caps() const override;
69
Timothy Liang7ac582e2018-08-06 09:47:23 -040070 void finalizeFragmentOutputColor(GrShaderVar& outputColor) override;
71
72 void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) override;
73
74 id<MTLLibrary> createMtlShaderLibrary(const GrGLSLShaderBuilder& builder,
75 SkSL::Program::Kind kind,
76 const SkSL::Program::Settings& settings,
77 GrProgramDesc* desc);
78
Jim Van Verth1223e7f2019-02-28 17:38:35 -050079 GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
80 const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
81 GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
Timothy Liang7ac582e2018-08-06 09:47:23 -040082
83 GrMtlGpu* fGpu;
Timothy Liang057c3902018-08-08 10:48:45 -040084 GrMtlUniformHandler fUniformHandler;
85 GrMtlVaryingHandler fVaryingHandler;
Timothy Liang7ac582e2018-08-06 09:47:23 -040086
87 typedef GrGLSLProgramBuilder INHERITED;
88};
89#endif