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 "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/GrMtlPipeline.h" 16 #include "src/gpu/mtl/GrMtlPipelineStateDataManager.h" 17 18 #import <Metal/Metal.h> 19 20 class GrMtlFramebuffer; 21 class GrMtlGpu; 22 class GrMtlPipelineStateDataManager; 23 class GrMtlRenderCommandEncoder; 24 class GrMtlRenderPipeline; 25 class GrMtlSampler; 26 class GrMtlTexture; 27 class GrPipeline; 28 29 /** 30 * Wraps a MTLRenderPipelineState object and also contains more info about the pipeline as needed 31 * by Ganesh 32 */ 33 class GrMtlPipelineState { 34 public: 35 using UniformInfoArray = GrMtlPipelineStateDataManager::UniformInfoArray; 36 using UniformHandle = GrGLSLProgramDataManager::UniformHandle; 37 38 GrMtlPipelineState(GrMtlGpu*, 39 sk_sp<GrMtlRenderPipeline> pipeline, 40 MTLPixelFormat, 41 const GrGLSLBuiltinUniformHandles& builtinUniformHandles, 42 const UniformInfoArray& uniforms, 43 uint32_t uniformBufferSize, 44 uint32_t numSamplers, 45 std::unique_ptr<GrGeometryProcessor::ProgramImpl>, 46 std::unique_ptr<GrXferProcessor::ProgramImpl>, 47 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls); 48 pipeline()49 const sk_sp<GrMtlRenderPipeline>& pipeline() const { return fPipeline; } 50 51 void setData(GrMtlFramebuffer*, const GrProgramInfo&); 52 53 void setTextures(const GrGeometryProcessor&, 54 const GrPipeline&, 55 const GrSurfaceProxy* const geomProcTextures[]); 56 void bindTextures(GrMtlRenderCommandEncoder* renderCmdEncoder); 57 58 void setDrawState(GrMtlRenderCommandEncoder*, 59 const GrSwizzle& writeSwizzle, 60 const GrXferProcessor&); 61 62 static void SetDynamicScissorRectState(GrMtlRenderCommandEncoder* renderCmdEncoder, 63 SkISize colorAttachmentDimensions, 64 GrSurfaceOrigin rtOrigin, 65 SkIRect scissorRect); 66 67 bool doesntSampleAttachment(const MTLRenderPassAttachmentDescriptor*) const; 68 69 private: 70 /** 71 * We use the RT's size and origin to adjust from Skia device space to Metal normalized device 72 * space and to make device space positions have the correct origin for processors that require 73 * them. 74 */ 75 struct RenderTargetState { 76 SkISize fRenderTargetSize; 77 GrSurfaceOrigin fRenderTargetOrigin; 78 RenderTargetStateRenderTargetState79 RenderTargetState() { this->invalidate(); } invalidateRenderTargetState80 void invalidate() { 81 fRenderTargetSize.fWidth = -1; 82 fRenderTargetSize.fHeight = -1; 83 fRenderTargetOrigin = (GrSurfaceOrigin)-1; 84 } 85 }; 86 87 void setRenderTargetState(SkISize colorAttachmentDimensions, GrSurfaceOrigin); 88 89 void bindUniforms(GrMtlRenderCommandEncoder*); 90 91 void setBlendConstants(GrMtlRenderCommandEncoder*, const GrSwizzle&, const GrXferProcessor&); 92 93 void setDepthStencilState(GrMtlRenderCommandEncoder* renderCmdEncoder); 94 95 struct SamplerBindings { 96 GrMtlSampler* fSampler; 97 id<MTLTexture> fTexture; 98 99 SamplerBindings(GrSamplerState state, GrTexture* texture, GrMtlGpu*); 100 }; 101 102 GrMtlGpu* fGpu; 103 sk_sp<GrMtlRenderPipeline> fPipeline; 104 MTLPixelFormat fPixelFormat; 105 106 RenderTargetState fRenderTargetState; 107 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; 108 109 GrStencilSettings fStencil; 110 111 int fNumSamplers; 112 SkTArray<SamplerBindings> fSamplerBindings; 113 114 std::unique_ptr<GrGeometryProcessor::ProgramImpl> fGPImpl; 115 std::unique_ptr<GrXferProcessor::ProgramImpl> fXPImpl; 116 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fFPImpls; 117 118 GrMtlPipelineStateDataManager fDataManager; 119 }; 120 121 #endif 122