Skip to content

Commit

Permalink
Allow WebGPU to compile non-WGSL source
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=247657
<radar://102116179>

Reviewed by NOBODY (OOPS!).

To facilitate testing, use the string passed to createShaderModule for
compiling the MTLLibrary if the WGSL compiler gives a zero length string.

* Source/WebGPU/WebGPU/ShaderModule.mm:
(WebGPU::earlyCompileShaderModule):
(WebGPU::Device::createShaderModule):

* Websites/webkit.org/demos/webgpu/scripts/hello-triangle.js:
(async helloTriangle):
  • Loading branch information
mwyrzykowski committed Nov 9, 2022
1 parent 052d5b6 commit a45ca42
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
19 changes: 4 additions & 15 deletions Source/WebGPU/WebGPU/ShaderModule.mm
Expand Up @@ -30,14 +30,6 @@
#import "Device.h"
#import "PipelineLayout.h"

#if ENABLE(WEBGPU_BY_DEFAULT) && __has_include("ShaderModuleSource.msl")
#define TEMPORARY_MSL_HACK_PLEASE_DELETE_ONCE_WGSL_COMPILER_IS_HOOKED_UP 1
#include "ShaderModuleSource.msl"
#else
#define TEMPORARY_MSL_HACK_PLEASE_DELETE_ONCE_WGSL_COMPILER_IS_HOOKED_UP 0
#define WEBGPU_SHADER_SOURCE ""
#endif

namespace WebGPU {

struct ShaderModuleParameters {
Expand Down Expand Up @@ -88,7 +80,7 @@
return library;
}

static RefPtr<ShaderModule> earlyCompileShaderModule(Device& device, std::variant<WGSL::SuccessfulCheck, WGSL::FailedCheck>&& checkResult, const WGPUShaderModuleDescriptorHints& suppliedHints, String&& label)
static RefPtr<ShaderModule> earlyCompileShaderModule(Device& device, std::variant<WGSL::SuccessfulCheck, WGSL::FailedCheck>&& checkResult, const WGPUShaderModuleDescriptorHints& suppliedHints, String&& label, const char* code)
{
HashMap<String, Ref<PipelineLayout>> hints;
HashMap<String, WGSL::PipelineLayout> wgslHints;
Expand All @@ -103,11 +95,8 @@
}

auto prepareResult = WGSL::prepare(std::get<WGSL::SuccessfulCheck>(checkResult).ast, wgslHints);
#if TEMPORARY_MSL_HACK_PLEASE_DELETE_ONCE_WGSL_COMPILER_IS_HOOKED_UP
auto prepareResultMSL = prepareResult.msl.length() ? prepareResult.msl : String::fromUTF8(WEBGPU_SHADER_SOURCE);
#else
auto prepareResultMSL = prepareResult.msl;
#endif
// FIXME: remove the ternary when the shader compiler is more functional
auto prepareResultMSL = prepareResult.msl.length() ? prepareResult.msl : String::fromUTF8(code);
auto library = ShaderModule::createLibrary(device.device(), prepareResultMSL, WTFMove(label));
if (!library)
return nullptr;
Expand All @@ -126,7 +115,7 @@
auto checkResult = WGSL::staticCheck(fromAPI(shaderModuleParameters->wgsl.code), std::nullopt);

if (std::holds_alternative<WGSL::SuccessfulCheck>(checkResult) && shaderModuleParameters->hints && shaderModuleParameters->hints->hintsCount) {
if (auto result = earlyCompileShaderModule(*this, WTFMove(checkResult), *shaderModuleParameters->hints, fromAPI(descriptor.label)))
if (auto result = earlyCompileShaderModule(*this, WTFMove(checkResult), *shaderModuleParameters->hints, fromAPI(descriptor.label), shaderModuleParameters->wgsl.code))
return result.releaseNonNull();
}

Expand Down
32 changes: 31 additions & 1 deletion Websites/webkit.org/demos/webgpu/scripts/hello-triangle.js
Expand Up @@ -27,7 +27,8 @@ async function helloTriangle() {
const uniformBindGroupLayout = device.createBindGroupLayout({ entries: [{binding: 0, visibility: GPUShaderStage.VERTEX, buffer: {}}] });
const pipelineLayoutDesc = { bindGroupLayouts: [uniformBindGroupLayout] };
const layout = device.createPipelineLayout(pipelineLayoutDesc);

/*
FIXME: Use WGSL once compiler is brought up
const wgslSource = `
@vertex fn main(@builtin(vertex_index) VertexIndex: u32) -> @builtin(position) vec4<f32>
{
Expand All @@ -44,6 +45,35 @@ async function helloTriangle() {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
`;
*/
const wgslSource = `
#include <metal_stdlib>
using namespace metal;
struct Vertex {
float4 position [[position]];
float4 color;
};
vertex Vertex vsmain(unsigned VertexIndex [[vertex_id]])
{
float2 pos[3] = {
float2( 0.0, 0.5),
float2(-0.5, -0.5),
float2( 0.5, -0.5),
};
Vertex vout;
vout.position = float4(pos[VertexIndex], 0.0, 1.0);
vout.color = float4(pos[VertexIndex] + float2(0.5, 0.5), 0.0, 1.0);
return vout;
}
fragment float4 fsmain(Vertex in [[stage_in]])
{
return in.color;
}
`;

const shaderModule = device.createShaderModule({ code: wgslSource, isWHLSL: false, hints: [ {layout: layout }, ] });

/* GPUPipelineStageDescriptors */
Expand Down

0 comments on commit a45ca42

Please sign in to comment.