1 /* 2 * Copyright 2022 Google LLC 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 skgpu_graphite_VulkanResourceProvider_DEFINED 9 #define skgpu_graphite_VulkanResourceProvider_DEFINED 10 11 #include "src/gpu/graphite/ResourceProvider.h" 12 #include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h" 13 14 #include "include/gpu/vk/VulkanTypes.h" 15 #include "src/core/SkLRUCache.h" 16 #include "src/core/SkTHash.h" 17 #include "src/gpu/graphite/DescriptorData.h" 18 19 #ifdef SK_BUILD_FOR_ANDROID 20 extern "C" { 21 typedef struct AHardwareBuffer AHardwareBuffer; 22 } 23 #endif 24 25 namespace skgpu::graphite { 26 27 class VulkanCommandBuffer; 28 class VulkanDescriptorSet; 29 class VulkanFramebuffer; 30 class VulkanGraphicsPipeline; 31 class VulkanRenderPass; 32 class VulkanSharedContext; 33 class VulkanYcbcrConversion; 34 35 class VulkanResourceProvider final : public ResourceProvider { 36 public: 37 static constexpr size_t kIntrinsicConstantSize = sizeof(float) * 8; // float4 + 2xfloat2 38 // Intrinsic constant rtAdjust value is needed by the vertex shader. Dst copy bounds are needed 39 // in the frag shader. 40 static constexpr VkShaderStageFlagBits kIntrinsicConstantStageFlags = 41 VkShaderStageFlagBits(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT); 42 43 static constexpr size_t kLoadMSAAPushConstantSize = sizeof(float) * 4; 44 static constexpr VkShaderStageFlagBits kLoadMSAAPushConstantStageFlags = 45 VK_SHADER_STAGE_VERTEX_BIT; 46 47 48 using UniformBindGroupKey = FixedSizeKey<2 * VulkanGraphicsPipeline::kNumUniformBuffers>; 49 50 VulkanResourceProvider(SharedContext* sharedContext, 51 SingleOwner*, 52 uint32_t recorderID, 53 size_t resourceBudget); 54 55 ~VulkanResourceProvider() override; 56 57 sk_sp<VulkanYcbcrConversion> findOrCreateCompatibleYcbcrConversion( 58 const VulkanYcbcrConversionInfo& ycbcrInfo) const; 59 60 private: 61 const VulkanSharedContext* vulkanSharedContext() const; 62 63 sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*, 64 const UniqueKey&, 65 const GraphicsPipelineDesc&, 66 const RenderPassDesc&, 67 SkEnumBitMask<PipelineCreationFlags>, 68 uint32_t compilationID) override; 69 sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override; 70 71 sk_sp<Texture> createTexture(SkISize, 72 const TextureInfo&, 73 skgpu::Budgeted) override; 74 sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) override; 75 sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) override; 76 sk_sp<Sampler> createSampler(const SamplerDesc&) override; 77 78 sk_sp<VulkanFramebuffer> createFramebuffer( 79 const VulkanSharedContext*, 80 const skia_private::TArray<VkImageView>& attachmentViews, 81 const VulkanRenderPass&, 82 const int width, 83 const int height); 84 85 BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override; 86 #ifdef SK_BUILD_FOR_ANDROID 87 BackendTexture onCreateBackendTexture(AHardwareBuffer*, 88 bool isRenderable, 89 bool isProtectedContent, 90 SkISize dimensions, 91 bool fromAndroidWindow) const override; 92 #endif 93 void onDeleteBackendTexture(const BackendTexture&) override; 94 95 sk_sp<VulkanDescriptorSet> findOrCreateDescriptorSet(SkSpan<DescriptorData>); 96 97 sk_sp<VulkanDescriptorSet> findOrCreateUniformBuffersDescriptorSet( 98 SkSpan<DescriptorData> requestedDescriptors, 99 SkSpan<BindBufferInfo> bindUniformBufferInfo); 100 101 sk_sp<VulkanGraphicsPipeline> findOrCreateLoadMSAAPipeline(const RenderPassDesc&); 102 103 // Find or create a compatible (needed when creating a framebuffer and graphics pipeline) or 104 // full (needed when beginning a render pass from the command buffer) RenderPass. 105 sk_sp<VulkanRenderPass> findOrCreateRenderPass(const RenderPassDesc&, bool compatibleOnly); 106 107 // Use a predetermined RenderPass key for finding/creating a RenderPass to avoid recreating it 108 sk_sp<VulkanRenderPass> findOrCreateRenderPassWithKnownKey( 109 const RenderPassDesc&, bool compatibleOnly, const GraphiteResourceKey& rpKey); 110 111 VkPipelineCache pipelineCache(); mockPushConstantPipelineLayout()112 VkPipelineLayout mockPushConstantPipelineLayout() const { 113 return fMockPushConstantPipelineLayout; 114 } 115 116 friend class VulkanCommandBuffer; 117 friend class VulkanGraphicsPipeline; 118 VkPipelineCache fPipelineCache = VK_NULL_HANDLE; 119 120 // Create and store a pipeline layout that has compatible push constant parameters with other 121 // real pipeline layouts that can be reused across command buffers. 122 VkPipelineLayout fMockPushConstantPipelineLayout; 123 124 // The first value of the pair is a renderpass key. Graphics pipeline keys contain extra 125 // information that we do not need for identifying unique pipelines. 126 skia_private::TArray<std::pair<GraphiteResourceKey, 127 sk_sp<VulkanGraphicsPipeline>>> fLoadMSAAPipelines; 128 // All of the following attributes are the same between all msaa load pipelines, so they only 129 // need to be created once and can then be stored. 130 VkShaderModule fMSAALoadVertShaderModule = VK_NULL_HANDLE; 131 VkShaderModule fMSAALoadFragShaderModule = VK_NULL_HANDLE; 132 VkPipelineShaderStageCreateInfo fMSAALoadShaderStageInfo[2]; 133 VkPipelineLayout fMSAALoadPipelineLayout = VK_NULL_HANDLE; 134 135 SkLRUCache<UniformBindGroupKey, sk_sp<VulkanDescriptorSet>, 136 UniformBindGroupKey::Hash> fUniformBufferDescSetCache; 137 }; 138 139 } // namespace skgpu::graphite 140 141 #endif // skgpu_graphite_VulkanResourceProvider_DEFINED 142