blob: 45a1d5af788458c959558ed67b30b6d97aaed0e4 [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
Timothy Liang057c3902018-08-08 10:48:45 -040011#include "GrMtlBuffer.h"
Timothy Liang6ed63962018-08-10 09:49:44 -040012#include "GrMtlPipelineStateDataManager.h"
Timothy Liangde0be802018-08-10 13:48:08 -040013#include "GrTypesPriv.h"
Timothy Liang6ed63962018-08-10 09:49:44 -040014#include "glsl/GrGLSLProgramBuilder.h"
Timothy Liang057c3902018-08-08 10:48:45 -040015
Timothy Liang7ac582e2018-08-06 09:47:23 -040016#import <metal/metal.h>
17
18class GrMtlGpu;
Timothy Liang6ed63962018-08-10 09:49:44 -040019class GrMtlPipelineStateDataManager;
20class GrMtlSampler;
21class GrMtlTexture;
22class GrPipeline;
Timothy Liang7ac582e2018-08-06 09:47:23 -040023
Timothy Liang057c3902018-08-08 10:48:45 -040024/**
25 * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed
26 * by Ganesh
27 */
Timothy Liang7ac582e2018-08-06 09:47:23 -040028class GrMtlPipelineState {
29public:
Timothy Liang6ed63962018-08-10 09:49:44 -040030 using UniformInfoArray = GrMtlPipelineStateDataManager::UniformInfoArray;
31 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
32
33 GrMtlPipelineState(
34 GrMtlGpu* gpu,
35 id<MTLRenderPipelineState> pipelineState,
36 MTLPixelFormat pixelFormat,
37 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
38 const UniformInfoArray& uniforms,
39 GrMtlBuffer* geometryUniformBuffer,
40 GrMtlBuffer* fragmentUniformBuffer,
41 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
Timothy Liang6ed63962018-08-10 09:49:44 -040049 void setData(const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline,
50 const GrTextureProxy* const primProcTextures[]);
Timothy Liang057c3902018-08-08 10:48:45 -040051
Timothy Liang6ed63962018-08-10 09:49:44 -040052 void bind(id<MTLRenderCommandEncoder>);
53
Timothy Liangde0be802018-08-10 13:48:08 -040054 void setBlendConstants(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&);
55
Timothy Liang6ed63962018-08-10 09:49:44 -040056private:
57 /**
58 * We use the RT's size and origin to adjust from Skia device space to Metal normalized device
59 * space and to make device space positions have the correct origin for processors that require
60 * them.
61 */
62 struct RenderTargetState {
63 SkISize fRenderTargetSize;
64 GrSurfaceOrigin fRenderTargetOrigin;
65
66 RenderTargetState() { this->invalidate(); }
67 void invalidate() {
68 fRenderTargetSize.fWidth = -1;
69 fRenderTargetSize.fHeight = -1;
70 fRenderTargetOrigin = (GrSurfaceOrigin)-1;
71 }
72
73 /**
74 * Gets a float4 that adjusts the position from Skia device coords to Metals normalized
75 * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec,
76 * v, is applied as such:
77 * pos.x = dot(v.xy, pos.xz)
78 * pos.y = dot(v.zw, pos.yz)
79 */
80 void getRTAdjustmentVec(float* destVec) {
81 destVec[0] = 2.f / fRenderTargetSize.fWidth;
82 destVec[1] = -1.f;
83 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
84 destVec[2] = -2.f / fRenderTargetSize.fHeight;
85 destVec[3] = 1.f;
86 } else {
87 destVec[2] = 2.f / fRenderTargetSize.fHeight;
88 destVec[3] = -1.f;
89 }
90 }
91 };
92
93 void setRenderTargetState(const GrRenderTargetProxy*);
94
95 struct SamplerBindings {
96 id<MTLSamplerState> fSampler;
97 id<MTLTexture> fTexture;
98 GrShaderFlags fVisibility;
99
100 SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrShaderFlags flags,
101 GrMtlGpu*);
102 };
103
104 GrMtlGpu* fGpu;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400105 id<MTLRenderPipelineState> fPipelineState;
Timothy Liang057c3902018-08-08 10:48:45 -0400106 MTLPixelFormat fPixelFormat;
Timothy Liang6ed63962018-08-10 09:49:44 -0400107
108 RenderTargetState fRenderTargetState;
109 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
110
111 sk_sp<GrMtlBuffer> fGeometryUniformBuffer;
112 sk_sp<GrMtlBuffer> fFragmentUniformBuffer;
113
114 int fNumSamplers;
115 SkTArray<SamplerBindings> fSamplerBindings;
116
117 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
118 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
119 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
120 int fFragmentProcessorCnt;
121
122 GrMtlPipelineStateDataManager fDataManager;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400123};
124
125#endif