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 11 #include "GrMtlBuffer.h" 12 #include "GrMtlPipelineStateDataManager.h" 13 #include "GrStencilSettings.h" 14 #include "GrTypesPriv.h" 15 #include "glsl/GrGLSLProgramBuilder.h" 16 17 #import <metal/metal.h> 18 19 class GrMtlGpu; 20 class GrMtlPipelineStateDataManager; 21 class GrMtlSampler; 22 class GrMtlTexture; 23 class GrPipeline; 24 25 /** 26 * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed 27 * by Ganesh 28 */ 29 class GrMtlPipelineState { 30 public: 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, 40 sk_sp<GrMtlBuffer> geometryUniformBuffer, 41 sk_sp<GrMtlBuffer> fragmentUniformBuffer, 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); 47 mtlPipelineState()48 id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; } 49 50 void setData(const GrRenderTarget*, GrSurfaceOrigin, 51 const GrPrimitiveProcessor& primPRoc, const GrPipeline& pipeline, 52 const GrTextureProxy* const primProcTextures[]); 53 54 void bind(id<MTLRenderCommandEncoder>); 55 56 void setBlendConstants(id<MTLRenderCommandEncoder>, GrPixelConfig, const GrXferProcessor&); 57 58 void setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder); 59 60 private: 61 /** 62 * We use the RT's size and origin to adjust from Skia device space to Metal normalized device 63 * space and to make device space positions have the correct origin for processors that require 64 * them. 65 */ 66 struct RenderTargetState { 67 SkISize fRenderTargetSize; 68 GrSurfaceOrigin fRenderTargetOrigin; 69 RenderTargetStateRenderTargetState70 RenderTargetState() { this->invalidate(); } invalidateRenderTargetState71 void invalidate() { 72 fRenderTargetSize.fWidth = -1; 73 fRenderTargetSize.fHeight = -1; 74 fRenderTargetOrigin = (GrSurfaceOrigin)-1; 75 } 76 77 /** 78 * Gets a float4 that adjusts the position from Skia device coords to Metals normalized 79 * device coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, 80 * v, is applied as such: 81 * pos.x = dot(v.xy, pos.xz) 82 * pos.y = dot(v.zw, pos.yz) 83 */ getRTAdjustmentVecRenderTargetState84 void getRTAdjustmentVec(float* destVec) { 85 destVec[0] = 2.f / fRenderTargetSize.fWidth; 86 destVec[1] = -1.f; 87 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { 88 destVec[2] = -2.f / fRenderTargetSize.fHeight; 89 destVec[3] = 1.f; 90 } else { 91 destVec[2] = 2.f / fRenderTargetSize.fHeight; 92 destVec[3] = -1.f; 93 } 94 } 95 }; 96 97 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); 98 99 struct SamplerBindings { 100 id<MTLSamplerState> fSampler; 101 id<MTLTexture> fTexture; 102 103 SamplerBindings(const GrSamplerState& state, GrTexture* texture, GrMtlGpu*); 104 }; 105 106 GrMtlGpu* fGpu; 107 id<MTLRenderPipelineState> fPipelineState; 108 MTLPixelFormat fPixelFormat; 109 110 RenderTargetState fRenderTargetState; 111 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; 112 113 GrStencilSettings fStencil; 114 115 sk_sp<GrMtlBuffer> fGeometryUniformBuffer; 116 sk_sp<GrMtlBuffer> fFragmentUniformBuffer; 117 118 int fNumSamplers; 119 SkTArray<SamplerBindings> fSamplerBindings; 120 121 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; 122 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; 123 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; 124 int fFragmentProcessorCnt; 125 126 GrMtlPipelineStateDataManager fDataManager; 127 }; 128 129 #endif 130