blob: e71aee2611321cea8922a97fb9b59a5693cd8bbe [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 GrMtlPipelineState_DEFINED
9#define GrMtlPipelineState_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrTypesPriv.h"
12#include "src/gpu/GrStencilSettings.h"
13#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14#include "src/gpu/mtl/GrMtlBuffer.h"
15#include "src/gpu/mtl/GrMtlPipelineStateDataManager.h"
Timothy Liang057c3902018-08-08 10:48:45 -040016
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020017#import <Metal/Metal.h>
Timothy Liang7ac582e2018-08-06 09:47:23 -040018
19class GrMtlGpu;
Timothy Liang6ed63962018-08-10 09:49:44 -040020class GrMtlPipelineStateDataManager;
21class GrMtlSampler;
22class GrMtlTexture;
23class GrPipeline;
Timothy Liang7ac582e2018-08-06 09:47:23 -040024
Timothy Liang057c3902018-08-08 10:48:45 -040025/**
26 * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed
27 * by Ganesh
28 */
Timothy Liang7ac582e2018-08-06 09:47:23 -040029class GrMtlPipelineState {
30public:
Timothy Liang6ed63962018-08-10 09:49:44 -040031 using UniformInfoArray = GrMtlPipelineStateDataManager::UniformInfoArray;
32 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
33
34 GrMtlPipelineState(
35 GrMtlGpu* gpu,
36 id<MTLRenderPipelineState> pipelineState,
37 MTLPixelFormat pixelFormat,
38 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
39 const UniformInfoArray& uniforms,
Ethan Nicholase5dc1eb2019-08-13 17:02:25 -040040 uint32_t uniformBufferSize,
Timothy Liang6ed63962018-08-10 09:49:44 -040041 uint32_t numSamplers,
42 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
43 std::unique_ptr<GrGLSLXferProcessor> xferPRocessor,
44 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
45 int fFragmentProcessorCnt);
Timothy Liang7ac582e2018-08-06 09:47:23 -040046
47 id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; }
48
Robert Phillips901aff02019-10-08 12:32:56 -040049 void setData(const GrRenderTarget*, const GrProgramInfo&);
Timothy Liang057c3902018-08-08 10:48:45 -040050
Greg Daniel2c3398d2019-06-19 11:58:01 -040051 void setDrawState(id<MTLRenderCommandEncoder>, const GrSwizzle& outputSwizzle,
52 const GrXferProcessor&);
Ethan Nicholas01063512018-10-08 16:58:25 -040053
Jim Van Verth686046b2019-03-18 15:39:22 -040054 static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder,
55 const GrRenderTarget* renderTarget,
56 GrSurfaceOrigin rtOrigin,
57 SkIRect scissorRect);
58
Jim Van Verthf3b27b42019-04-24 14:29:37 -040059 bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const;
60
Timothy Liang6ed63962018-08-10 09:49:44 -040061private:
62 /**
63 * We use the RT's size and origin to adjust from Skia device space to Metal normalized device
64 * space and to make device space positions have the correct origin for processors that require
65 * them.
66 */
67 struct RenderTargetState {
68 SkISize fRenderTargetSize;
69 GrSurfaceOrigin fRenderTargetOrigin;
70
71 RenderTargetState() { this->invalidate(); }
72 void invalidate() {
73 fRenderTargetSize.fWidth = -1;
74 fRenderTargetSize.fHeight = -1;
75 fRenderTargetOrigin = (GrSurfaceOrigin)-1;
76 }
77
78 /**
79 * Gets a float4 that adjusts the position from Skia device coords to Metals normalized
80 * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec,
81 * v, is applied as such:
82 * pos.x = dot(v.xy, pos.xz)
83 * pos.y = dot(v.zw, pos.yz)
84 */
85 void getRTAdjustmentVec(float* destVec) {
86 destVec[0] = 2.f / fRenderTargetSize.fWidth;
87 destVec[1] = -1.f;
88 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
89 destVec[2] = -2.f / fRenderTargetSize.fHeight;
90 destVec[3] = 1.f;
91 } else {
92 destVec[2] = 2.f / fRenderTargetSize.fHeight;
93 destVec[3] = -1.f;
94 }
95 }
96 };
97
Robert Phillipsd0fe8752019-01-31 14:13:59 -050098 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
Timothy Liang6ed63962018-08-10 09:49:44 -040099
Jim Van Verthba91f652019-03-19 12:18:31 -0400100 void bind(id<MTLRenderCommandEncoder>);
101
Greg Daniel2c3398d2019-06-19 11:58:01 -0400102 void setBlendConstants(id<MTLRenderCommandEncoder>, const GrSwizzle&, const GrXferProcessor&);
Jim Van Verthba91f652019-03-19 12:18:31 -0400103
104 void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder);
105
Timothy Liang6ed63962018-08-10 09:49:44 -0400106 struct SamplerBindings {
Jim Van Verth75c53262019-04-26 12:23:51 -0400107 GrMtlSampler* fSampler;
Timothy Liang6ed63962018-08-10 09:49:44 -0400108 id<MTLTexture> fTexture;
Timothy Liang6ed63962018-08-10 09:49:44 -0400109
Greg Daniel0f70be82018-10-08 17:35:08 +0000110 SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*);
Timothy Liang6ed63962018-08-10 09:49:44 -0400111 };
112
113 GrMtlGpu* fGpu;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400114 id<MTLRenderPipelineState> fPipelineState;
Timothy Liang057c3902018-08-08 10:48:45 -0400115 MTLPixelFormat fPixelFormat;
Timothy Liang6ed63962018-08-10 09:49:44 -0400116
117 RenderTargetState fRenderTargetState;
118 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
119
Ethan Nicholas01063512018-10-08 16:58:25 -0400120 GrStencilSettings fStencil;
121
Timothy Liang6ed63962018-08-10 09:49:44 -0400122 int fNumSamplers;
123 SkTArray<SamplerBindings> fSamplerBindings;
124
125 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
126 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
127 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
128 int fFragmentProcessorCnt;
129
130 GrMtlPipelineStateDataManager fDataManager;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400131};
132
133#endif