Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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 Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 16 | |
Aaron O'Mullan | 829b6a0 | 2019-07-08 01:31:14 +0200 | [diff] [blame] | 17 | #import <Metal/Metal.h> |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 18 | |
| 19 | class GrMtlGpu; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 20 | class GrMtlPipelineStateDataManager; |
| 21 | class GrMtlSampler; |
| 22 | class GrMtlTexture; |
| 23 | class GrPipeline; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 24 | |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 25 | /** |
| 26 | * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed |
| 27 | * by Ganesh |
| 28 | */ |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 29 | class GrMtlPipelineState { |
| 30 | public: |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 31 | using UniformInfoArray = GrMtlPipelineStateDataManager::UniformInfoArray; |
| 32 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 33 | |
| 34 | GrMtlPipelineState( |
| 35 | GrMtlGpu* gpu, |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 36 | id<MTLRenderPipelineState> pipelineState, |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 37 | MTLPixelFormat pixelFormat, |
| 38 | const GrGLSLBuiltinUniformHandles& builtinUniformHandles, |
| 39 | const UniformInfoArray& uniforms, |
Ethan Nicholas | e5dc1eb | 2019-08-13 17:02:25 -0400 | [diff] [blame] | 40 | uint32_t uniformBufferSize, |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 41 | uint32_t numSamplers, |
| 42 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
Brian Salomon | 17f95b1 | 2021-02-23 09:35:08 -0500 | [diff] [blame] | 43 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
| 44 | std::vector<std::unique_ptr<GrGLSLFragmentProcessor>> fpImpls); |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 45 | |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 46 | id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; } |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 47 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 48 | void setData(const GrRenderTarget*, const GrProgramInfo&); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 49 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 50 | void setTextures(const GrPrimitiveProcessor&, const GrPipeline&, const GrSurfaceProxy* const[]); |
Jim Van Verth | 9042d46 | 2019-11-18 11:58:25 -0500 | [diff] [blame] | 51 | void bindTextures(id<MTLRenderCommandEncoder> renderCmdEncoder); |
| 52 | |
Brian Salomon | 982f546 | 2020-03-30 12:52:33 -0400 | [diff] [blame] | 53 | void setDrawState(id<MTLRenderCommandEncoder>, |
| 54 | const GrSwizzle& writeSwizzle, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 55 | const GrXferProcessor&); |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 56 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 57 | static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder, |
| 58 | const GrRenderTarget* renderTarget, |
| 59 | GrSurfaceOrigin rtOrigin, |
| 60 | SkIRect scissorRect); |
| 61 | |
Jim Van Verth | f3b27b4 | 2019-04-24 14:29:37 -0400 | [diff] [blame] | 62 | bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const; |
| 63 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 64 | private: |
| 65 | /** |
| 66 | * We use the RT's size and origin to adjust from Skia device space to Metal normalized device |
| 67 | * space and to make device space positions have the correct origin for processors that require |
| 68 | * them. |
| 69 | */ |
| 70 | struct RenderTargetState { |
| 71 | SkISize fRenderTargetSize; |
| 72 | GrSurfaceOrigin fRenderTargetOrigin; |
| 73 | |
| 74 | RenderTargetState() { this->invalidate(); } |
| 75 | void invalidate() { |
| 76 | fRenderTargetSize.fWidth = -1; |
| 77 | fRenderTargetSize.fHeight = -1; |
| 78 | fRenderTargetOrigin = (GrSurfaceOrigin)-1; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets a float4 that adjusts the position from Skia device coords to Metals normalized |
| 83 | * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, |
| 84 | * v, is applied as such: |
| 85 | * pos.x = dot(v.xy, pos.xz) |
| 86 | * pos.y = dot(v.zw, pos.yz) |
| 87 | */ |
| 88 | void getRTAdjustmentVec(float* destVec) { |
| 89 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 90 | destVec[1] = -1.f; |
| 91 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 92 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 93 | destVec[3] = 1.f; |
| 94 | } else { |
| 95 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 96 | destVec[3] = -1.f; |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 101 | void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 102 | |
Jim Van Verth | 9042d46 | 2019-11-18 11:58:25 -0500 | [diff] [blame] | 103 | void bindUniforms(id<MTLRenderCommandEncoder>); |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 104 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 105 | void setBlendConstants(id<MTLRenderCommandEncoder>, const GrSwizzle&, const GrXferProcessor&); |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 106 | |
| 107 | void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder); |
| 108 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 109 | struct SamplerBindings { |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 110 | GrMtlSampler* fSampler; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 111 | id<MTLTexture> fTexture; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 112 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 113 | SamplerBindings(GrSamplerState state, GrTexture* texture, GrMtlGpu*); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | GrMtlGpu* fGpu; |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 117 | id<MTLRenderPipelineState> fPipelineState; |
| 118 | MTLPixelFormat fPixelFormat; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 119 | |
| 120 | RenderTargetState fRenderTargetState; |
| 121 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
| 122 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 123 | GrStencilSettings fStencil; |
| 124 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 125 | int fNumSamplers; |
| 126 | SkTArray<SamplerBindings> fSamplerBindings; |
| 127 | |
| 128 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 129 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 17f95b1 | 2021-02-23 09:35:08 -0500 | [diff] [blame] | 130 | std::vector<std::unique_ptr<GrGLSLFragmentProcessor>> fFPImpls; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 131 | |
| 132 | GrMtlPipelineStateDataManager fDataManager; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif |