[bazel] Add support for Dawn (via Vulkan)

sk_app has existing support for Dawn on top of Vulkan, and
this adds support to build //example:hello_world_dawn and
get this to run on Linux.

Dawn depends on Tint and abseil-cpp. Tint further depends on
spirv_tools and spirv_headers (for writing to the SPIR-V format).
Dawn and Tint only have GN and CMake support, so we need to make
our Bazel rules for them (see //third_party/BUILD.bazel).

abseil-cpp and the SPIR-V libraries have Bazel support, so we
can just include them (see //WORKSPACE.bazel). It is important
that @spirv_headers be called that exactly because @spirv_tools
depends on it by that name.

The hand-crafted cc_library rules for Dawn and Tint were produced
by reading the appropriate GN files and using the parts necessary
for a supporting Vulkan+Linux. If we use Dawn for other backends
(e.g. WebGPU), we will need to expand the Bazel rules. One day,
we might contribute the Bazel rules to Dawn and Tint so they
can support them and avoid breaking us if new files are added.

Suggested Review Order
 - bazel/common_config_settings/BUILD.bazel to see introduction
   of new select-able option "has_gpu_backend" which cleans up
   some of our code that is enabled for any GPU backend.
 - src/*/BUILD.bazel to see has_gpu_backend rolled out.
 - WORKSPACE.bazel to see DEPS declared there (using the files
   in third_party/externals, which are brought in via
   tools/git-sync-deps).
 - third_party/BUILD.bazel which adds Dawn and Tint rules.
   It may be helpful to look in third_party/externals for
   the Dawn [1] and Tint [2] GN files. Especially interesting
   are the Python scripts [3] Dawn uses to generate some
   header and source files.
 - All other files.

[1] https://dawn.googlesource.com/dawn/+/d9f22ce0346b222759d5510be3d1cd93caa5ab86/src/dawn/native/BUILD.gn#183
[2] https://dawn.googlesource.com/tint/+/453d5ae84ec30ab51ac592c13d472412ae8b5fc9/src/tint/BUILD.gn#174
[3] https://dawn.googlesource.com/dawn/+/d9f22ce0346b222759d5510be3d1cd93caa5ab86/generator/dawn_json_generator.py#774
Change-Id: Ied5b162045d8e841b9666457f0158457e2b078d4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/516996
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
This commit is contained in:
Kevin Lubick 2022-03-21 13:06:32 -04:00 committed by SkCQ
parent ffc96dada4
commit 14abec45f0
12 changed files with 1162 additions and 21 deletions

View File

@ -53,9 +53,8 @@ CORE_DEPS = [
"//src/shaders:srcs",
"//src/utils:srcs",
"//third_party:skcms",
] + selects.with_or({
# https://github.com/bazelbuild/bazel-skylib/blob/main/docs/selects_doc.md#selectswith_or
("//bazel/common_config_settings:gl_backend", "//bazel/common_config_settings:vulkan_backend"): [
] + select({
"//bazel/common_config_settings:has_gpu_backend": [
"//src/gpu:srcs",
"//src/sksl:srcs",
# TODO(kjlubick) should mock be test only?

View File

@ -136,3 +136,22 @@ browser_repositories(
chromium = True,
firefox = True,
)
###################################################
# External C++ deps with Bazel support #
###################################################
local_repository(
name = "abseil_cpp",
path = "third_party/externals/abseil-cpp",
)
local_repository(
name = "spirv_tools",
path = "third_party/externals/spirv-tools",
)
local_repository(
# This name is important because spirv_tools expects @spirv_headers to exist by that name.
name = "spirv_headers",
path = "third_party/externals/spirv-headers",
)

View File

@ -37,6 +37,11 @@ GPU_DEFINES = select({
"SK_SUPPORT_GPU=1",
"SK_USE_VMA", # TODO(kjlubick) This will need to be configurable
],
"//bazel/common_config_settings:dawn_backend": [
"SK_DAWN",
"SK_SUPPORT_GPU=1",
"VK_USE_PLATFORM_XCB_KHR", # TODO(kjlubick) support dawn's dawn_enable_vulkan etc
],
"//conditions:default": [
"SK_SUPPORT_GPU=0",
],

View File

@ -1,5 +1,8 @@
load(":defs.bzl", "bool_flag", "string_flag_with_values")
# https://github.com/bazelbuild/bazel-skylib
load("@bazel_skylib//lib:selects.bzl", "selects")
# @platforms is found at https://github.com/bazelbuild/platforms
package(default_visibility = ["//:__subpackages__"])
@ -59,6 +62,15 @@ config_setting(
},
)
selects.config_setting_group(
name = "has_gpu_backend",
match_any = [
"//bazel/common_config_settings:gl_backend",
"//bazel/common_config_settings:dawn_backend",
"//bazel/common_config_settings:vulkan_backend",
],
)
# =============================================================================
# Configurable Skia Features
# =============================================================================
@ -81,6 +93,7 @@ string_flag_with_values(
values = [
"gl_backend",
"vulkan_backend",
"dawn_backend",
],
)

View File

@ -66,6 +66,36 @@ cc_binary_with_flags(
],
)
cc_binary_with_flags(
name = "hello_world_dawn",
srcs = [
"//example:HelloWorld_src",
],
linkopts = [
"-lX11",
"-lxcb",
"-lXau",
"-lXdmcp",
"-lX11-xcb",
],
# These flags are defined in //bazel/common_config_settings/BUILD.bazel
set_flags = {
"gpu_backend": [
"dawn_backend",
],
# Load fonts from the standard system directory (e.g. "/usr/share/fonts/")
# as defined in //src/ports/SkFontMgr_custom_directory_factory.cpp
"fontmgr_factory": [
"custom_directory_fontmgr_factory",
],
},
deps = [
"//:skia_core",
"//src/utils:json_srcs",
"//tools/sk_app",
],
)
# Everything below this line is autogenerated by gazelle using the C++ plugin built here:
# https://github.com/google/skia-buildbot/tree/main/bazel/gazelle/cpp
# These can be regenerated (if needed) by navigating to //bazel and running `make generate`

View File

@ -24,6 +24,8 @@ HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
: fBackendType(Window::kNativeGL_BackendType),
#elif defined(SK_VULKAN)
: fBackendType(Window::kVulkan_BackendType),
#elif defined(SK_DAWN)
: fBackendType(Window::kDawn_BackendType),
#else
: fBackendType(Window::kRaster_BackendType),
#endif
@ -57,6 +59,8 @@ void HelloWorld::updateTitle() {
title.append("GL");
#elif defined(SK_VULKAN)
title.append("Vulkan");
#elif defined(SK_DAWN)
title.append("Dawn");
#else
title.append("Unknown GPU backend");
#endif
@ -133,6 +137,8 @@ bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
fBackendType = Window::kNativeGL_BackendType;
#elif defined(SK_VULKAN)
fBackendType = Window::kVulkan_BackendType;
#elif defined(SK_DAWN)
fBackendType = Window::kDawn_BackendType;
#else
SkDebugf("No GPU backend configured\n");
return true;

View File

@ -1,5 +1,3 @@
# https://github.com/bazelbuild/bazel-skylib
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//bazel:macros.bzl", "generated_cc_atom")
cc_library(
@ -223,8 +221,8 @@ cc_library(
cc_library(
name = "srcs",
visibility = ["//:__subpackages__"],
deps = [":core_srcs"] + selects.with_or({
("//bazel/common_config_settings:gl_backend", "//bazel/common_config_settings:vulkan_backend"): [
deps = [":core_srcs"] + select({
"//bazel/common_config_settings:has_gpu_backend": [
":sksl_srcs",
],
"//conditions:default": [],

View File

@ -209,6 +209,25 @@ cc_library(
],
)
cc_library(
name = "dawn_srcs",
deps = [
"//src/gpu/dawn:GrDawnAttachment_src",
"//src/gpu/dawn:GrDawnBuffer_src",
"//src/gpu/dawn:GrDawnCaps_src",
"//src/gpu/dawn:GrDawnGpu_src",
"//src/gpu/dawn:GrDawnOpsRenderPass_src",
"//src/gpu/dawn:GrDawnProgramBuilder_src",
"//src/gpu/dawn:GrDawnProgramDataManager_src",
"//src/gpu/dawn:GrDawnRenderTarget_src",
"//src/gpu/dawn:GrDawnRingBuffer_src",
"//src/gpu/dawn:GrDawnTextureRenderTarget_src",
"//src/gpu/dawn:GrDawnTexture_src",
"//src/gpu/dawn:GrDawnTypesPriv_src",
"//src/gpu/dawn:GrDawnUtil_src",
],
)
cc_library(
name = "gl_srcs",
deps = [
@ -334,6 +353,10 @@ cc_library(
name = "srcs",
visibility = ["//:__subpackages__"],
deps = [":core_srcs"] + select({
"//bazel/common_config_settings:dawn_backend": [
":dawn_srcs",
":v1_srcs",
],
"//bazel/common_config_settings:gl_backend": [
":gl_srcs",
":v1_srcs",

View File

@ -1,5 +1,3 @@
# https://github.com/bazelbuild/bazel-skylib
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//bazel:macros.bzl", "generated_cc_atom")
cc_library(
@ -27,8 +25,8 @@ cc_library(
cc_library(
name = "srcs",
visibility = ["//:__subpackages__"],
deps = [":core_srcs"] + selects.with_or({
("//bazel/common_config_settings:gl_backend", "//bazel/common_config_settings:vulkan_backend"): [
deps = [":core_srcs"] + select({
"//bazel/common_config_settings:has_gpu_backend": [
":gpu_srcs",
],
"//conditions:default": [],

1052
third_party/BUILD.bazel vendored

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
#define DawnTestContext_DEFINED
#include "tools/gpu/TestContext.h"
#include <dawn/native/DawnNative.h>
#include "dawn/native/DawnNative.h"
#ifdef SK_DAWN

View File

@ -3,9 +3,9 @@ load("@bazel_skylib//lib:selects.bzl", "selects")
load("//bazel:macros.bzl", "generated_cc_atom")
selects.config_setting_group(
name = "vulkan_unix",
name = "dawn_unix",
match_all = [
"//bazel/common_config_settings:vulkan_backend",
"//bazel/common_config_settings:dawn_backend",
"@platforms//os:linux",
],
)
@ -18,6 +18,14 @@ selects.config_setting_group(
],
)
selects.config_setting_group(
name = "vulkan_unix",
match_all = [
"//bazel/common_config_settings:vulkan_backend",
"@platforms//os:linux",
],
)
cc_library(
name = "sk_app",
visibility = ["//:__subpackages__"],
@ -36,6 +44,9 @@ cc_library(
],
# TODO(kjlubick) add Windows/Mac support
}) + select({
"//bazel/common_config_settings:dawn_backend": [
":DawnWindowContext_src",
],
"//bazel/common_config_settings:gl_backend": [
":GLWindowContext_src",
],
@ -44,6 +55,7 @@ cc_library(
"//tools/gpu/vk:VkTestUtils_src",
],
}) + select({
":dawn_unix": ["//tools/sk_app/unix:DawnVulkanWindowContext_unix_src"],
":gl_unix": ["//tools/sk_app/unix:GLWindowContext_unix_src"],
":vulkan_unix": ["//tools/sk_app/unix:VulkanWindowContext_unix_src"],
}),