blob: 5bbd5db15f1d1e3f519ad9d6e7789bc3c01b2e2d [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
Timothy Liang7ac582e2018-08-06 09:47:23 -040017#import <metal/metal.h>
18
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,
Jim Van Verthdd15d692019-04-22 15:29:53 -040040 uint32_t geometryUniformBufferSize,
41 uint32_t fragmentUniformBufferSize,
Timothy Liang6ed63962018-08-10 09:49:44 -040042 uint32_t numSamplers,
43 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
44 std::unique_ptr<GrGLSLXferProcessor> xferPRocessor,
45 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
46 int fFragmentProcessorCnt);
Timothy Liang7ac582e2018-08-06 09:47:23 -040047
48 id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; }
49
Robert Phillipsd0fe8752019-01-31 14:13:59 -050050 void setData(const GrRenderTarget*, GrSurfaceOrigin,
51 const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline,
Timothy Liang6ed63962018-08-10 09:49:44 -040052 const GrTextureProxy* const primProcTextures[]);
Timothy Liang057c3902018-08-08 10:48:45 -040053
Jim Van Verthba91f652019-03-19 12:18:31 -040054 void setDrawState(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&);
Ethan Nicholas01063512018-10-08 16:58:25 -040055
Jim Van Verth686046b2019-03-18 15:39:22 -040056 static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder,
57 const GrRenderTarget* renderTarget,
58 GrSurfaceOrigin rtOrigin,
59 SkIRect scissorRect);
60
Jim Van Verthf3b27b42019-04-24 14:29:37 -040061 bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const;
62
Timothy Liang6ed63962018-08-10 09:49:44 -040063private:
64 /**
65 * We use the RT's size and origin to adjust from Skia device space to Metal normalized device
66 * space and to make device space positions have the correct origin for processors that require
67 * them.
68 */
69 struct RenderTargetState {
70 SkISize fRenderTargetSize;
71 GrSurfaceOrigin fRenderTargetOrigin;
72
73 RenderTargetState() { this->invalidate(); }
74 void invalidate() {
75 fRenderTargetSize.fWidth = -1;
76 fRenderTargetSize.fHeight = -1;
77 fRenderTargetOrigin = (GrSurfaceOrigin)-1;
78 }
79
80 /**
81 * Gets a float4 that adjusts the position from Skia device coords to Metals normalized
82 * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec,
83 * v, is applied as such:
84 * pos.x = dot(v.xy, pos.xz)
85 * pos.y = dot(v.zw, pos.yz)
86 */
87 void getRTAdjustmentVec(float* destVec) {
88 destVec[0] = 2.f / fRenderTargetSize.fWidth;
89 destVec[1] = -1.f;
90 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
91 destVec[2] = -2.f / fRenderTargetSize.fHeight;
92 destVec[3] = 1.f;
93 } else {
94 destVec[2] = 2.f / fRenderTargetSize.fHeight;
95 destVec[3] = -1.f;
96 }
97 }
98 };
99
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500100 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
Timothy Liang6ed63962018-08-10 09:49:44 -0400101
Jim Van Verthba91f652019-03-19 12:18:31 -0400102 void bind(id<MTLRenderCommandEncoder>);
103
104 void setBlendConstants(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&);
105
106 void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder);
107
Timothy Liang6ed63962018-08-10 09:49:44 -0400108 struct SamplerBindings {
Jim Van Verth75c53262019-04-26 12:23:51 -0400109 GrMtlSampler* fSampler;
Timothy Liang6ed63962018-08-10 09:49:44 -0400110 id<MTLTexture> fTexture;
Timothy Liang6ed63962018-08-10 09:49:44 -0400111
Greg Daniel0f70be82018-10-08 17:35:08 +0000112 SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*);
Timothy Liang6ed63962018-08-10 09:49:44 -0400113 };
114
115 GrMtlGpu* fGpu;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400116 id<MTLRenderPipelineState> fPipelineState;
Timothy Liang057c3902018-08-08 10:48:45 -0400117 MTLPixelFormat fPixelFormat;
Timothy Liang6ed63962018-08-10 09:49:44 -0400118
119 RenderTargetState fRenderTargetState;
120 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
121
Ethan Nicholas01063512018-10-08 16:58:25 -0400122 GrStencilSettings fStencil;
123
Timothy Liang6ed63962018-08-10 09:49:44 -0400124 int fNumSamplers;
125 SkTArray<SamplerBindings> fSamplerBindings;
126
127 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
128 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
129 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
130 int fFragmentProcessorCnt;
131
132 GrMtlPipelineStateDataManager fDataManager;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400133};
134
135#endif