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 | |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 11 | #include "GrMtlBuffer.h" |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 12 | #include "GrMtlPipelineStateDataManager.h" |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 13 | #include "GrTypesPriv.h" |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramBuilder.h" |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 15 | |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 16 | #import <metal/metal.h> |
| 17 | |
| 18 | class GrMtlGpu; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 19 | class GrMtlPipelineStateDataManager; |
| 20 | class GrMtlSampler; |
| 21 | class GrMtlTexture; |
| 22 | class GrPipeline; |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 23 | |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 24 | /** |
| 25 | * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed |
| 26 | * by Ganesh |
| 27 | */ |
Timothy Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 28 | class GrMtlPipelineState { |
| 29 | public: |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 30 | 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 Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 46 | |
| 47 | id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; } |
| 48 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 49 | void setData(const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline, |
| 50 | const GrTextureProxy* const primProcTextures[]); |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 51 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 52 | void bind(id<MTLRenderCommandEncoder>); |
| 53 | |
Timothy Liang | de0be80 | 2018-08-10 13:48:08 -0400 | [diff] [blame] | 54 | void setBlendConstants(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&); |
| 55 | |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 56 | private: |
| 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 Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 105 | id<MTLRenderPipelineState> fPipelineState; |
Timothy Liang | 057c390 | 2018-08-08 10:48:45 -0400 | [diff] [blame] | 106 | MTLPixelFormat fPixelFormat; |
Timothy Liang | 6ed6396 | 2018-08-10 09:49:44 -0400 | [diff] [blame] | 107 | |
| 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 Liang | 7ac582e | 2018-08-06 09:47:23 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #endif |