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, |
| 36 | id<MTLRenderPipelineState> pipelineState, |
| 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, |
| 43 | std::unique_ptr<GrGLSLXferProcessor> xferPRocessor, |
| 44 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors, |
| 45 | int fFragmentProcessorCnt); |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 46 | |
| 47 | id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; } |
| 48 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 49 | void setData(const GrRenderTarget*, const GrProgramInfo&); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 50 | |
Jim Van Verth | 9042d46 | 2019-11-18 11:58:25 -0500 | [diff] [blame] | 51 | void setTextures(const GrProgramInfo& programInfo, |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 52 | const GrSurfaceProxy* const primProcTextures[]); |
Jim Van Verth | 9042d46 | 2019-11-18 11:58:25 -0500 | [diff] [blame] | 53 | void bindTextures(id<MTLRenderCommandEncoder> renderCmdEncoder); |
| 54 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 55 | void setDrawState(id<MTLRenderCommandEncoder>, const GrSwizzle& outputSwizzle, |
| 56 | const GrXferProcessor&); |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 57 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 58 | static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder, |
| 59 | const GrRenderTarget* renderTarget, |
| 60 | GrSurfaceOrigin rtOrigin, |
| 61 | SkIRect scissorRect); |
| 62 | |
Jim Van Verth | f3b27b4 | 2019-04-24 14:29:37 -0400 | [diff] [blame] | 63 | bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const; |
| 64 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 65 | private: |
| 66 | /** |
| 67 | * We use the RT's size and origin to adjust from Skia device space to Metal normalized device |
| 68 | * space and to make device space positions have the correct origin for processors that require |
| 69 | * them. |
| 70 | */ |
| 71 | struct RenderTargetState { |
| 72 | SkISize fRenderTargetSize; |
| 73 | GrSurfaceOrigin fRenderTargetOrigin; |
| 74 | |
| 75 | RenderTargetState() { this->invalidate(); } |
| 76 | void invalidate() { |
| 77 | fRenderTargetSize.fWidth = -1; |
| 78 | fRenderTargetSize.fHeight = -1; |
| 79 | fRenderTargetOrigin = (GrSurfaceOrigin)-1; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Gets a float4 that adjusts the position from Skia device coords to Metals normalized |
| 84 | * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, |
| 85 | * v, is applied as such: |
| 86 | * pos.x = dot(v.xy, pos.xz) |
| 87 | * pos.y = dot(v.zw, pos.yz) |
| 88 | */ |
| 89 | void getRTAdjustmentVec(float* destVec) { |
| 90 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 91 | destVec[1] = -1.f; |
| 92 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 93 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 94 | destVec[3] = 1.f; |
| 95 | } else { |
| 96 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 97 | destVec[3] = -1.f; |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 102 | void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 103 | |
Jim Van Verth | 9042d46 | 2019-11-18 11:58:25 -0500 | [diff] [blame] | 104 | void bindUniforms(id<MTLRenderCommandEncoder>); |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 105 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 106 | void setBlendConstants(id<MTLRenderCommandEncoder>, const GrSwizzle&, const GrXferProcessor&); |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 107 | |
| 108 | void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder); |
| 109 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 110 | struct SamplerBindings { |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 111 | GrMtlSampler* fSampler; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 112 | id<MTLTexture> fTexture; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 113 | |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 114 | SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | GrMtlGpu* fGpu; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 118 | id<MTLRenderPipelineState> fPipelineState; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 119 | MTLPixelFormat fPixelFormat; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 120 | |
| 121 | RenderTargetState fRenderTargetState; |
| 122 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
| 123 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 124 | GrStencilSettings fStencil; |
| 125 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 126 | int fNumSamplers; |
| 127 | SkTArray<SamplerBindings> fSamplerBindings; |
| 128 | |
| 129 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 130 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
| 131 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 132 | int fFragmentProcessorCnt; |
| 133 | |
| 134 | GrMtlPipelineStateDataManager fDataManager; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | #endif |