| 1 |
/* |
| 2 |
* Copyright 2015 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 GrVkUtil_DEFINED |
| 9 |
#define GrVkUtil_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "include/private/base/SkDebug.h" |
| 13 |
#include "include/private/base/SkMacros.h" |
| 14 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 15 |
#include "include/private/gpu/vk/SkiaVulkan.h" |
| 16 |
#include "src/gpu/vk/VulkanInterface.h" |
| 17 |
#include "src/sksl/ir/SkSLProgram.h" |
| 18 |
|
| 19 |
#include <string> |
| 20 |
|
| 21 |
class GrVkGpu; |
| 22 |
namespace SkSL { |
| 23 |
struct NativeShader; |
| 24 |
struct ProgramSettings; |
| 25 |
} // namespace SkSL |
| 26 |
|
| 27 |
// Uncomment to log all Ganesh Vulkan calls |
| 28 |
// #define GR_VK_DEBUG_LOG(X) SkDebugf("vk%s (%s:%d)\n", #X, __FILE__, __LINE__) |
| 29 |
|
| 30 |
// Or uncomment to trace all Ganesh Vulkan calls |
| 31 |
// TODO(b/471244369): expose this functionality as a build argument. |
| 32 |
// #include "src/core/SkTraceEvent.h" |
| 33 |
// #define GR_VK_DEBUG_LOG(X) \ |
| 34 |
// TRACE_EVENT1_ALWAYS("skia.gpu", "vk" #X, "line", __FILE__ ":" SK_MACRO_STRINGIFY(__LINE__)) |
| 35 |
|
| 36 |
// Makes a Vk call on the interface |
| 37 |
#ifdef GR_VK_DEBUG_LOG |
| 38 |
#define GR_VK_CALL(IFACE, X) \ |
| 39 |
([&]() { \ |
| 40 |
GR_VK_DEBUG_LOG(X); \ |
| 41 |
return (IFACE)->fFunctions.f##X; \ |
| 42 |
})() |
| 43 |
#else |
| 44 |
#define GR_VK_CALL(IFACE, X) (IFACE)->fFunctions.f##X |
| 45 |
#endif |
| 46 |
|
| 47 |
// Note: must be called before checkVkResult, since this does not log if the GPU is already |
| 48 |
// considering the device to be lost. |
| 49 |
#define GR_VK_LOG_IF_NOT_SUCCESS(GPU, RESULT, X, ...) \ |
| 50 |
do { \ |
| 51 |
if (RESULT != VK_SUCCESS && !GPU->isDeviceLost()) { \ |
| 52 |
SkDebugf("Failed vulkan call. Error: %d, " X "\n", RESULT, ##__VA_ARGS__); \ |
| 53 |
} \ |
| 54 |
} while (false) |
| 55 |
|
| 56 |
#define GR_VK_CALL_RESULT(GPU, RESULT, X) \ |
| 57 |
do { \ |
| 58 |
(RESULT) = GR_VK_CALL(GPU->vkInterface(), X); \ |
| 59 |
SkASSERT(VK_SUCCESS == RESULT || VK_ERROR_DEVICE_LOST == RESULT); \ |
| 60 |
GR_VK_LOG_IF_NOT_SUCCESS(GPU, RESULT, #X); \ |
| 61 |
GPU->checkVkResult(RESULT); \ |
| 62 |
} while (false) |
| 63 |
|
| 64 |
#define GR_VK_CALL_RESULT_NOCHECK(GPU, RESULT, X) \ |
| 65 |
do { \ |
| 66 |
(RESULT) = GR_VK_CALL(GPU->vkInterface(), X); \ |
| 67 |
GPU->checkVkResult(RESULT); \ |
| 68 |
} while (false) |
| 69 |
|
| 70 |
// same as GR_VK_CALL but checks for success |
| 71 |
#define GR_VK_CALL_ERRCHECK(GPU, X) \ |
| 72 |
VkResult SK_MACRO_APPEND_LINE(ret); \ |
| 73 |
GR_VK_CALL_RESULT(GPU, SK_MACRO_APPEND_LINE(ret), X) |
| 74 |
|
| 75 |
|
| 76 |
bool GrVkFormatIsSupported(VkFormat); |
| 77 |
|
| 78 |
static constexpr GrColorFormatDesc GrVkFormatDesc(VkFormat vkFormat) { |
| 79 |
switch (vkFormat) { |
| 80 |
case VK_FORMAT_R8G8B8A8_UNORM: |
| 81 |
return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm); |
| 82 |
case VK_FORMAT_R8_UNORM: |
| 83 |
return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm); |
| 84 |
case VK_FORMAT_B8G8R8A8_UNORM: |
| 85 |
return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm); |
| 86 |
case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 87 |
return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm); |
| 88 |
case VK_FORMAT_B5G6R5_UNORM_PACK16: |
| 89 |
return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm); |
| 90 |
case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 91 |
return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat); |
| 92 |
case VK_FORMAT_R16_SFLOAT: |
| 93 |
return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat); |
| 94 |
case VK_FORMAT_R8G8B8_UNORM: |
| 95 |
return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm); |
| 96 |
case VK_FORMAT_R8G8_UNORM: |
| 97 |
return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm); |
| 98 |
case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 99 |
return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm); |
| 100 |
case VK_FORMAT_A2R10G10B10_UNORM_PACK32: |
| 101 |
return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm); |
| 102 |
case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 103 |
return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm); |
| 104 |
case VK_FORMAT_R4G4B4A4_UNORM_PACK16: |
| 105 |
return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm); |
| 106 |
case VK_FORMAT_R8G8B8A8_SRGB: |
| 107 |
return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm); |
| 108 |
case VK_FORMAT_R16_UNORM: |
| 109 |
return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm); |
| 110 |
case VK_FORMAT_R16G16_UNORM: |
| 111 |
return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm); |
| 112 |
case VK_FORMAT_R16G16B16A16_UNORM: |
| 113 |
return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm); |
| 114 |
case VK_FORMAT_R16G16_SFLOAT: |
| 115 |
return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat); |
| 116 |
|
| 117 |
// Compressed texture formats are not expected to have a description. |
| 118 |
case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid(); |
| 119 |
case VK_FORMAT_BC1_RGB_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid(); |
| 120 |
case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: return GrColorFormatDesc::MakeInvalid(); |
| 121 |
|
| 122 |
// This type only describes color channels. |
| 123 |
case VK_FORMAT_S8_UINT: return GrColorFormatDesc::MakeInvalid(); |
| 124 |
case VK_FORMAT_D24_UNORM_S8_UINT: return GrColorFormatDesc::MakeInvalid(); |
| 125 |
case VK_FORMAT_D32_SFLOAT_S8_UINT: return GrColorFormatDesc::MakeInvalid(); |
| 126 |
|
| 127 |
default: return GrColorFormatDesc::MakeInvalid(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
bool GrCompileVkShaderModule(GrVkGpu* gpu, |
| 132 |
const std::string& shaderString, |
| 133 |
VkShaderStageFlagBits stage, |
| 134 |
VkShaderModule* shaderModule, |
| 135 |
VkPipelineShaderStageCreateInfo* stageInfo, |
| 136 |
const SkSL::ProgramSettings& settings, |
| 137 |
SkSL::NativeShader* outSPIRV, |
| 138 |
SkSL::Program::Interface* outInterface); |
| 139 |
|
| 140 |
bool GrInstallVkShaderModule(GrVkGpu* gpu, |
| 141 |
const SkSL::NativeShader& spirv, |
| 142 |
VkShaderStageFlagBits stage, |
| 143 |
VkShaderModule* shaderModule, |
| 144 |
VkPipelineShaderStageCreateInfo* stageInfo); |
| 145 |
|
| 146 |
#endif |