| 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 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkStream.h" |
| 11 |
#include "include/gpu/ShaderErrorHandler.h" |
| 12 |
#include "include/gpu/graphite/Context.h" |
| 13 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 14 |
#include "src/core/SkTraceEvent.h" |
| 15 |
#include "src/gpu/graphite/ContextPriv.h" |
| 16 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 17 |
#include "src/gpu/graphite/TextureFormat.h" |
| 18 |
#include "src/gpu/graphite/vk/VulkanQueueManager.h" |
| 19 |
#include "src/gpu/graphite/vk/VulkanSampler.h" |
| 20 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 21 |
#include "src/sksl/SkSLProgramSettings.h" |
| 22 |
|
| 23 |
namespace skgpu::graphite::ContextFactory { |
| 24 |
|
| 25 |
std::unique_ptr<Context> MakeVulkan(const VulkanBackendContext& backendContext, |
| 26 |
const ContextOptions& options) { |
| 27 |
sk_sp<SharedContext> sharedContext = VulkanSharedContext::Make(backendContext, options); |
| 28 |
if (!sharedContext) { |
| 29 |
return nullptr; |
| 30 |
} |
| 31 |
|
| 32 |
std::unique_ptr<QueueManager> queueManager(new VulkanQueueManager(backendContext.fQueue, |
| 33 |
sharedContext.get())); |
| 34 |
if (!queueManager) { |
| 35 |
return nullptr; |
| 36 |
} |
| 37 |
|
| 38 |
return ContextCtorAccessor::MakeContext(std::move(sharedContext), |
| 39 |
std::move(queueManager), |
| 40 |
options); |
| 41 |
} |
| 42 |
|
| 43 |
} // namespace skgpu::graphite::ContextFactory |
| 44 |
|
| 45 |
namespace skgpu::graphite { |
| 46 |
|
| 47 |
VkShaderModule CreateVulkanShaderModule(const VulkanSharedContext* context, |
| 48 |
const SkSL::NativeShader& spirv, |
| 49 |
VkShaderStageFlagBits stage) { |
| 50 |
TRACE_EVENT0("skia.shaders", "InstallVkShaderModule"); |
| 51 |
VkShaderModuleCreateInfo moduleCreateInfo = {}; |
| 52 |
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 53 |
moduleCreateInfo.codeSize = spirv.fBinary.size() * sizeof(uint32_t); |
| 54 |
moduleCreateInfo.pCode = spirv.fBinary.data(); |
| 55 |
|
| 56 |
VkShaderModule shaderModule; |
| 57 |
VkResult result; |
| 58 |
VULKAN_CALL_RESULT(context, |
| 59 |
result, |
| 60 |
CreateShaderModule(context->device(), |
| 61 |
&moduleCreateInfo, |
| 62 |
/*const VkAllocationCallbacks*=*/nullptr, |
| 63 |
&shaderModule)); |
| 64 |
if (result != VK_SUCCESS) { |
| 65 |
SKGPU_LOG_E("Failed to create VkShaderModule"); |
| 66 |
return VK_NULL_HANDLE; |
| 67 |
} |
| 68 |
return shaderModule; |
| 69 |
} |
| 70 |
|
| 71 |
void DescriptorDataToVkDescSetLayout(const VulkanSharedContext* ctxt, |
| 72 |
const SkSpan<DescriptorData>& requestedDescriptors, |
| 73 |
VkDescriptorSetLayout* outLayout) { |
| 74 |
// If requestedDescriptors is empty, that simply means we should create an empty placeholder |
| 75 |
// layout that doesn't actually contain any descriptors. |
| 76 |
skia_private::STArray<kDescriptorTypeCount, VkDescriptorSetLayoutBinding> bindingLayouts; |
| 77 |
for (size_t i = 0; i < requestedDescriptors.size(); i++) { |
| 78 |
if (requestedDescriptors[i].fCount != 0) { |
| 79 |
const DescriptorData& currDescriptor = requestedDescriptors[i]; |
| 80 |
VkDescriptorSetLayoutBinding& layoutBinding = bindingLayouts.push_back(); |
| 81 |
layoutBinding = {}; |
| 82 |
layoutBinding.binding = currDescriptor.fBindingIndex; |
| 83 |
layoutBinding.descriptorType = DsTypeEnumToVkDs(currDescriptor.fType); |
| 84 |
layoutBinding.descriptorCount = currDescriptor.fCount; |
| 85 |
layoutBinding.stageFlags = |
| 86 |
PipelineStageFlagsToVkShaderStageFlags(currDescriptor.fPipelineStageFlags); |
| 87 |
layoutBinding.pImmutableSamplers = currDescriptor.fImmutableSampler |
| 88 |
? (static_cast<const VulkanSampler*>( |
| 89 |
currDescriptor.fImmutableSampler))->constVkSamplerPtr() |
| 90 |
: nullptr; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
VkDescriptorSetLayoutCreateInfo layoutCreateInfo = {}; |
| 95 |
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 96 |
layoutCreateInfo.bindingCount = bindingLayouts.size(); |
| 97 |
layoutCreateInfo.pBindings = bindingLayouts.data(); |
| 98 |
|
| 99 |
VkResult result; |
| 100 |
VULKAN_CALL_RESULT( |
| 101 |
ctxt, |
| 102 |
result, |
| 103 |
CreateDescriptorSetLayout(ctxt->device(), &layoutCreateInfo, nullptr, outLayout)); |
| 104 |
if (result != VK_SUCCESS) { |
| 105 |
SkDebugf("Failed to create VkDescriptorSetLayout\n"); |
| 106 |
outLayout = VK_NULL_HANDLE; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
VkDescriptorType DsTypeEnumToVkDs(DescriptorType type) { |
| 111 |
switch (type) { |
| 112 |
case DescriptorType::kUniformBuffer: |
| 113 |
return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; |
| 114 |
case DescriptorType::kTextureSampler: |
| 115 |
return VK_DESCRIPTOR_TYPE_SAMPLER; |
| 116 |
case DescriptorType::kTexture: |
| 117 |
return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 118 |
case DescriptorType::kCombinedTextureSampler: |
| 119 |
return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 120 |
case DescriptorType::kStorageBuffer: |
| 121 |
return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC; |
| 122 |
case DescriptorType::kInputAttachment: |
| 123 |
return VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT; |
| 124 |
} |
| 125 |
SkUNREACHABLE; |
| 126 |
} |
| 127 |
|
| 128 |
#define VK_FORMAT_MAPPING(M) \ |
| 129 |
M(TextureFormat::kR8, VK_FORMAT_R8_UNORM) \ |
| 130 |
M(TextureFormat::kR16, VK_FORMAT_R16_UNORM) \ |
| 131 |
M(TextureFormat::kR16F, VK_FORMAT_R16_SFLOAT) \ |
| 132 |
M(TextureFormat::kR32F, VK_FORMAT_R32_SFLOAT) \ |
| 133 |
/*TextureFormat::kA8 unsupported */ \ |
| 134 |
M(TextureFormat::kRG8, VK_FORMAT_R8G8_UNORM) \ |
| 135 |
M(TextureFormat::kRG16, VK_FORMAT_R16G16_UNORM) \ |
| 136 |
M(TextureFormat::kRG16F, VK_FORMAT_R16G16_SFLOAT) \ |
| 137 |
M(TextureFormat::kRG32F, VK_FORMAT_R32G32_SFLOAT) \ |
| 138 |
M(TextureFormat::kRGB8, VK_FORMAT_R8G8B8_UNORM) \ |
| 139 |
M(TextureFormat::kBGR8, VK_FORMAT_B8G8R8_UNORM) \ |
| 140 |
M(TextureFormat::kB5_G6_R5, VK_FORMAT_R5G6B5_UNORM_PACK16) \ |
| 141 |
M(TextureFormat::kR5_G6_B5, VK_FORMAT_B5G6R5_UNORM_PACK16) \ |
| 142 |
M(TextureFormat::kRGB16, VK_FORMAT_R16G16B16_UNORM) \ |
| 143 |
M(TextureFormat::kRGB16F, VK_FORMAT_R16G16B16_SFLOAT) \ |
| 144 |
M(TextureFormat::kRGB32F, VK_FORMAT_R32G32B32_SFLOAT) \ |
| 145 |
M(TextureFormat::kRGB8_sRGB, VK_FORMAT_R8G8B8_SRGB) \ |
| 146 |
/*TextureFormat::kBGR10_XR unsupported */ \ |
| 147 |
M(TextureFormat::kRGBA8, VK_FORMAT_R8G8B8A8_UNORM) \ |
| 148 |
M(TextureFormat::kRGBA16, VK_FORMAT_R16G16B16A16_UNORM) \ |
| 149 |
M(TextureFormat::kRGBA16F, VK_FORMAT_R16G16B16A16_SFLOAT) \ |
| 150 |
M(TextureFormat::kRGBA32F, VK_FORMAT_R32G32B32A32_SFLOAT) \ |
| 151 |
M(TextureFormat::kRGB10_A2, VK_FORMAT_A2B10G10R10_UNORM_PACK32) \ |
| 152 |
M(TextureFormat::kRGBA10x6, VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16) \ |
| 153 |
M(TextureFormat::kRGBA8_sRGB, VK_FORMAT_R8G8B8A8_SRGB) \ |
| 154 |
M(TextureFormat::kBGRA8, VK_FORMAT_B8G8R8A8_UNORM) \ |
| 155 |
M(TextureFormat::kBGR10_A2, VK_FORMAT_A2R10G10B10_UNORM_PACK32) \ |
| 156 |
M(TextureFormat::kBGRA8_sRGB, VK_FORMAT_B8G8R8A8_SRGB) \ |
| 157 |
M(TextureFormat::kABGR4, VK_FORMAT_R4G4B4A4_UNORM_PACK16) \ |
| 158 |
M(TextureFormat::kARGB4, VK_FORMAT_B4G4R4A4_UNORM_PACK16) \ |
| 159 |
/*TextureFormat::kBGRA10x6_XR unsupported */ \ |
| 160 |
M(TextureFormat::kRGB8_ETC2, VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK) \ |
| 161 |
M(TextureFormat::kRGB8_ETC2_sRGB, VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK) \ |
| 162 |
M(TextureFormat::kRGB8_BC1, VK_FORMAT_BC1_RGB_UNORM_BLOCK) \ |
| 163 |
M(TextureFormat::kRGBA8_BC1, VK_FORMAT_BC1_RGBA_UNORM_BLOCK) \ |
| 164 |
M(TextureFormat::kRGBA8_BC1_sRGB, VK_FORMAT_BC1_RGBA_SRGB_BLOCK) \ |
| 165 |
M(TextureFormat::kYUV8_P2_420, VK_FORMAT_G8_B8R8_2PLANE_420_UNORM) \ |
| 166 |
M(TextureFormat::kYUV8_P3_420, VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM) \ |
| 167 |
M(TextureFormat::kYUV10x6_P2_420, VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16) \ |
| 168 |
/*TextureFormat::kExternal VK_FORMAT_UNDEFINED w/ uint64_t sidecar */ \ |
| 169 |
M(TextureFormat::kS8, VK_FORMAT_S8_UINT) \ |
| 170 |
M(TextureFormat::kD16, VK_FORMAT_D16_UNORM) \ |
| 171 |
M(TextureFormat::kD32F, VK_FORMAT_D32_SFLOAT) \ |
| 172 |
M(TextureFormat::kD24_S8, VK_FORMAT_D24_UNORM_S8_UINT) \ |
| 173 |
M(TextureFormat::kD32F_S8, VK_FORMAT_D32_SFLOAT_S8_UINT) |
| 174 |
|
| 175 |
// NOTE: For external formats, Vulkan stores the value outside of VkFormat and expects the VkFormat |
| 176 |
// to be VK_FORMAT_UNDEFINED. The mapping functions below are unaware of this side car, so it |
| 177 |
// maps VK_FORMAT_UNDEFINED to TextureFormat::kUnsupported (this is disambiguated explicitly by |
| 178 |
// VulkanTextureInfo which has the full context). The reverse maps TextureFormat::kExternal to |
| 179 |
// VK_FORMAT_UNDEFINED, as expected, although callers are responsible for passing along the actual |
| 180 |
// external format separately. |
| 181 |
|
| 182 |
TextureFormat VkFormatToTextureFormat(VkFormat format) { |
| 183 |
#define M(TF, VK) case VK: return TF; |
| 184 |
switch(format) { |
| 185 |
VK_FORMAT_MAPPING(M) |
| 186 |
default: return TextureFormat::kUnsupported; |
| 187 |
} |
| 188 |
#undef M |
| 189 |
} |
| 190 |
VkFormat TextureFormatToVkFormat(TextureFormat format) { |
| 191 |
#define M(TF, VK) case TF: return VK; |
| 192 |
switch(format) { |
| 193 |
VK_FORMAT_MAPPING(M) |
| 194 |
default: return VK_FORMAT_UNDEFINED; |
| 195 |
} |
| 196 |
#undef M |
| 197 |
} |
| 198 |
|
| 199 |
VkImageAspectFlags GetVkImageAspectFlags(TextureFormat format) { |
| 200 |
switch (format) { |
| 201 |
case TextureFormat::kS8: |
| 202 |
return VK_IMAGE_ASPECT_STENCIL_BIT; |
| 203 |
case TextureFormat::kD16: |
| 204 |
[[fallthrough]]; |
| 205 |
case TextureFormat::kD32F: |
| 206 |
return VK_IMAGE_ASPECT_DEPTH_BIT; |
| 207 |
case TextureFormat::kD24_S8: |
| 208 |
[[fallthrough]]; |
| 209 |
case TextureFormat::kD32F_S8: |
| 210 |
return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 211 |
default: |
| 212 |
return VK_IMAGE_ASPECT_COLOR_BIT; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
VkShaderStageFlags PipelineStageFlagsToVkShaderStageFlags( |
| 217 |
SkEnumBitMask<PipelineStageFlags> stageFlags) { |
| 218 |
VkShaderStageFlags vkStageFlags = 0; |
| 219 |
if (stageFlags & PipelineStageFlags::kVertexShader) { |
| 220 |
vkStageFlags |= VK_SHADER_STAGE_VERTEX_BIT; |
| 221 |
} |
| 222 |
if (stageFlags & PipelineStageFlags::kFragmentShader) { |
| 223 |
vkStageFlags |= VK_SHADER_STAGE_FRAGMENT_BIT; |
| 224 |
} |
| 225 |
if (stageFlags & PipelineStageFlags::kCompute) { |
| 226 |
vkStageFlags |= VK_SHADER_STAGE_COMPUTE_BIT; |
| 227 |
} |
| 228 |
return vkStageFlags; |
| 229 |
} |
| 230 |
|
| 231 |
bool RenderPassDescWillLoadMSAAFromResolve(const RenderPassDesc& renderPassDesc) { |
| 232 |
return renderPassDesc.fColorResolveAttachment.fFormat != TextureFormat::kUnsupported && |
| 233 |
renderPassDesc.fColorResolveAttachment.fLoadOp == LoadOp::kLoad; |
| 234 |
} |
| 235 |
|
| 236 |
bool RenderPassDescWillImplicitlyLoadMSAA(const RenderPassDesc& renderPassDesc) { |
| 237 |
SkASSERT(renderPassDesc.fColorAttachment.fFormat != TextureFormat::kUnsupported); |
| 238 |
SkASSERT(renderPassDesc.fColorAttachment.fSampleCount > SampleCount::k1 || |
| 239 |
renderPassDesc.fColorResolveAttachment.fFormat == TextureFormat::kUnsupported); |
| 240 |
|
| 241 |
return renderPassDesc.fColorAttachment.fSampleCount == SampleCount::k1 && |
| 242 |
renderPassDesc.fSampleCount > SampleCount::k1; |
| 243 |
} |
| 244 |
|
| 245 |
RenderPassDesc MakePipelineCompatibleRenderPass(const RenderPassDesc& renderPassDesc) { |
| 246 |
RenderPassDesc compatible = renderPassDesc; |
| 247 |
|
| 248 |
// Reset all load/store ops on the attachments since those do not affect compatibility. |
| 249 |
// Choose a combination that is most likely to be used later by the real render pass. |
| 250 |
const bool hasResolve = |
| 251 |
renderPassDesc.fColorResolveAttachment.fFormat != TextureFormat::kUnsupported; |
| 252 |
const bool needLoadMSAAFromResolveSubpass = |
| 253 |
RenderPassDescWillLoadMSAAFromResolve(renderPassDesc); |
| 254 |
compatible.fColorAttachment.fLoadOp = |
| 255 |
needLoadMSAAFromResolveSubpass ? LoadOp::kDiscard : LoadOp::kClear; |
| 256 |
compatible.fColorAttachment.fStoreOp = hasResolve ? StoreOp::kDiscard : StoreOp::kStore; |
| 257 |
compatible.fColorResolveAttachment.fLoadOp = |
| 258 |
needLoadMSAAFromResolveSubpass ? LoadOp::kLoad : LoadOp::kDiscard; |
| 259 |
compatible.fColorResolveAttachment.fStoreOp = hasResolve ? StoreOp::kStore : StoreOp::kDiscard; |
| 260 |
compatible.fDepthStencilAttachment.fLoadOp = LoadOp::kClear; |
| 261 |
compatible.fDepthStencilAttachment.fStoreOp = StoreOp::kDiscard; |
| 262 |
|
| 263 |
return compatible; |
| 264 |
} |
| 265 |
|
| 266 |
} // namespace skgpu::graphite |