22#include <emscripten/bind.h>
23#include <emscripten/emscripten.h>
24#include <emscripten/html5.h>
28#include <emscripten/html5_webgpu.h>
34#include <webgpu/webgpu.h>
37#include <webgpu/webgpu_cpp.h>
40 std::string canvasSelector,
43 wgpu::SurfaceDescriptorFromCanvasHTMLSelector surfaceSelector;
44 surfaceSelector.selector = canvasSelector.c_str();
46 wgpu::SurfaceDescriptor surface_desc;
47 surface_desc.nextInChain = &surfaceSelector;
51 wgpu::SwapChainDescriptor swap_chain_desc;
52 swap_chain_desc.format = wgpu::TextureFormat::BGRA8Unorm;
53 swap_chain_desc.usage = wgpu::TextureUsage::RenderAttachment;
54 swap_chain_desc.presentMode = wgpu::PresentMode::Fifo;
55 swap_chain_desc.width =
width;
56 swap_chain_desc.height =
height;
77 wgpu::Device
device = wgpu::Device::Acquire(emscripten_webgpu_get_device());
80 SkDebugf(
"Could not create GrDirectContext\n");
85 "uniform float2 iResolution;"
86 "uniform float iTime;"
89 " return step(max(d.x, d.y), a);"
91 "half4 main(float2 C) {"
93 " C.y = iResolution.y - C.y;"
94 " for (float i = 0; i < 3; ++i) {"
95 " vec2 U = C.yx / iResolution.yx;"
97 " U.x = U.x * .4 + U.y * U.y;"
98 " U.y += U.x * sin(-iTime * 9. + i * 2. + U.x * 25.) * .2;"
99 " U.x -= asin(sin(U.y * 34.))/20.;"
101 " O += .3 * vec4(.8 * b(.3) + b(.2), b(.2), b(.1), -1.);"
108 SkDebugf(
"Failed to compile SkSL: %s\n", err.c_str());
124 GrDawnRenderTargetInfo rtInfo;
125 rtInfo.fTextureView = fCanvasSwapChain.GetCurrentTextureView();
126 rtInfo.fFormat = wgpu::TextureFormat::BGRA8Unorm;
127 rtInfo.fLevelCount = 1;
154 bool flipColor = fFrameCount % 2 == 0;
159 bool flipColor = fFrameCount % 2 == 0;
163 float x = (
float)fWidth / 2.f;
164 float y = (
float)fHeight / 2.f;
167 flipColor ? colors1 : colors2,
175 uniforms.
width = fWidth;
176 uniforms.
height = fHeight;
177 uniforms.
time =
static_cast<float>(timestamp) / 1000.f;
181 paint->setShader(shader);
188 wgpu::SwapChain fCanvasSwapChain;
195 emscripten::enum_<DemoKind>(
"DemoKind")
199 emscripten::class_<Demo>(
"Demo")
@ kTopLeft_GrSurfaceOrigin
constexpr SkColor SK_ColorMAGENTA
constexpr SkColor SK_ColorCYAN
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static wgpu::SwapChain getSwapChainForCanvas(wgpu::Device device, std::string canvasSelector, int width, int height)
EMSCRIPTEN_BINDINGS(Skia)
void drawGradient(SkPaint *paint)
void drawSolidColor(SkPaint *paint)
bool init(std::string canvasSelector, int width, int height)
void setKind(DemoKind kind)
void drawRuntimeEffect(SkPaint *paint, int timestamp)
void flushAndSubmit(GrSyncCpu sync=GrSyncCpu::kNo)
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
static sk_sp< SkShader > MakeRadial(const SkPoint ¢er, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const
static Result MakeForShader(SkString sksl, const Options &)
static float min(float r, float g, float b)
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API std::unique_ptr< Context > MakeDawn(const DawnBackendContext &, const ContextOptions &)
static constexpr SkPoint Make(float x, float y)