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 | |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 17 | #import <metal/metal.h> |
| 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, |
Jim Van Verth | dd15d69 | 2019-04-22 15:29:53 -0400 | [diff] [blame] | 40 | uint32_t geometryUniformBufferSize, |
| 41 | uint32_t fragmentUniformBufferSize, |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 42 | 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 Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 47 | |
| 48 | id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; } |
| 49 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 50 | void setData(const GrRenderTarget*, GrSurfaceOrigin, |
| 51 | const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline, |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 52 | const GrTextureProxy* const primProcTextures[]); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 53 | |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 54 | void setDrawState(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&); |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 55 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 56 | static void SetDynamicScissorRectState(id<MTLRenderCommandEncoder> renderCmdEncoder, |
| 57 | const GrRenderTarget* renderTarget, |
| 58 | GrSurfaceOrigin rtOrigin, |
| 59 | SkIRect scissorRect); |
| 60 | |
Jim Van Verth | f3b27b4 | 2019-04-24 14:29:37 -0400 | [diff] [blame] | 61 | bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const; |
| 62 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 63 | private: |
| 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 Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 100 | void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 101 | |
Jim Van Verth | ba91f65 | 2019-03-19 12:18:31 -0400 | [diff] [blame] | 102 | void bind(id<MTLRenderCommandEncoder>); |
| 103 | |
| 104 | void setBlendConstants(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&); |
| 105 | |
| 106 | void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder); |
| 107 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 108 | struct SamplerBindings { |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 109 | GrMtlSampler* fSampler; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 110 | id<MTLTexture> fTexture; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 111 | |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 112 | SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*); |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | GrMtlGpu* fGpu; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 116 | id<MTLRenderPipelineState> fPipelineState; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 117 | MTLPixelFormat fPixelFormat; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 118 | |
| 119 | RenderTargetState fRenderTargetState; |
| 120 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
| 121 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 122 | GrStencilSettings fStencil; |
| 123 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 124 | 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 Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif |