[WHLSL] Allow vertex attributes to have arbitrary names in the shader
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 May 2019 23:38:18 +0000 (23:38 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 May 2019 23:38:18 +0000 (23:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=198235

Reviewed by Dean Jackson and Justin Fan.

Source/WebCore:

Metal doesn't allow arbitrary vertex attribute IDs. If you try to create a vertex attribute > 16,
the Metal validation layer will assert. So, we need to have a mapping from whatever the WebGPU
API says the vertex attribute IDs should be to the internally-used vertex attribute IDs.

Test: webgpu/whlsl-arbitrary-vertex-attribute-locations.html

* Modules/webgpu/WHLSL/Metal/WHLSLEntryPointScaffolding.cpp:
(WebCore::WHLSL::Metal::VertexEntryPointScaffolding::VertexEntryPointScaffolding):
* Modules/webgpu/WHLSL/WHLSLPipelineDescriptor.h:
* Modules/webgpu/WHLSL/WHLSLSemanticMatcher.cpp:
(WebCore::WHLSL::matchVertexAttributes):
* platform/graphics/gpu/cocoa/GPURenderPipelineMetal.mm:
(WebCore::convertRenderPipelineDescriptor):
(WebCore::trySetInputStateForPipelineDescriptor):

LayoutTests:

* webgpu/whlsl-arbitrary-vertex-attribute-locations-expected.html: Added.
* webgpu/whlsl-arbitrary-vertex-attribute-locations.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@245759 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations-expected.html [new file with mode: 0644]
LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLEntryPointScaffolding.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLPipelineDescriptor.h
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSemanticMatcher.cpp
Source/WebCore/platform/graphics/gpu/cocoa/GPURenderPipelineMetal.mm

index 6078db0..03874e0 100644 (file)
@@ -1,3 +1,13 @@
+2019-05-24  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [WHLSL] Allow vertex attributes to have arbitrary names in the shader
+        https://bugs.webkit.org/show_bug.cgi?id=198235
+
+        Reviewed by Dean Jackson and Justin Fan.
+
+        * webgpu/whlsl-arbitrary-vertex-attribute-locations-expected.html: Added.
+        * webgpu/whlsl-arbitrary-vertex-attribute-locations.html: Added.
+
 2019-05-24  Shawn Roberts  <sroberts@apple.com>
 
         scrollingcoordinator/scrolling-tree/fixed-inside-frame.html is no longer flaky
diff --git a/LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations-expected.html b/LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations-expected.html
new file mode 100644 (file)
index 0000000..f417050
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+<canvas id="canvas" width="400" height="400"></canvas>
+<script>
+async function start() {
+    const canvas = document.getElementById("canvas");
+    const context = canvas.getContext("2d");
+    context.fillStyle = "blue";
+    context.fillRect(0, 0, 400, 400);
+    context.fillStyle = "white";
+    context.fillRect(100, 100, 200, 200);
+}
+window.addEventListener("load", start);
+</script>
+</body>
+</html>
diff --git a/LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations.html b/LayoutTests/webgpu/whlsl-arbitrary-vertex-attribute-locations.html
new file mode 100644 (file)
index 0000000..40a1404
--- /dev/null
@@ -0,0 +1,124 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+<canvas id="canvas" width="400" height="400"></canvas>
+<script>
+const shaderSource = `
+struct VertexOut {
+    float4 position : SV_Position;
+    float shade : attribute(0);
+}
+
+vertex VertexOut vertexShader(float4 position : attribute(173), float shade : attribute(498)) {
+    VertexOut result;
+    result.position = position;
+    result.shade = shade;
+    return result;
+}
+
+fragment float4 fragmentShader(float shade : attribute(0)) : SV_Target 0 {
+    return float4(shade, shade, shade, 1.0);
+}
+`;
+async function start() {
+    const adapter = await navigator.gpu.requestAdapter();
+    const device = await adapter.requestDevice();
+
+    const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
+    const vertexStage = {module: shaderModule, entryPoint: "vertexShader"};
+    const fragmentStage = {module: shaderModule, entryPoint: "fragmentShader"};
+    const primitiveTopology = "triangle-strip";
+    const rasterizationState = {frontFace: "cw", cullMode: "none"};
+    const alphaBlend = {};
+    const colorBlend = {};
+    const colorStates = [{format: "rgba8unorm", alphaBlend, colorBlend, writeMask: 15}]; // GPUColorWriteBits.ALL
+    const depthStencilState = null;
+
+    const attribute0 = {shaderLocation: 173, inputSlot: 0, format: "float4", offset: 0};
+    const attribute1 = {shaderLocation: 498, inputSlot: 0, format: "float", offset: 16};
+    const attributes = [attribute0, attribute1];
+    const input0 = {inputSlot: 0, stride: 20 };
+    const inputs = [input0];
+    const inputState = {indexFormat: "uint32", attributes, inputs};
+
+    const bindGroupLayoutDescriptor = {bindings: [{binding: 0, visibility: 7, type: "uniform-buffer"}]};
+    const bindGroupLayout = device.createBindGroupLayout(bindGroupLayoutDescriptor);
+    const pipelineLayoutDescriptor = {bindGroupLayouts: [bindGroupLayout]};
+    const pipelineLayout = device.createPipelineLayout(pipelineLayoutDescriptor);
+
+    const renderPipelineDescriptor = {vertexStage, fragmentStage, primitiveTopology, rasterizationState, colorStates, depthStencilState, inputState, sampleCount: 1, layout: pipelineLayout};
+    const renderPipeline = device.createRenderPipeline(renderPipelineDescriptor);
+
+    const vertexBuffer0Descriptor = {size: Float32Array.BYTES_PER_ELEMENT * 5 * 4, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.MAP_WRITE};
+    const vertexBuffer0 = device.createBuffer(vertexBuffer0Descriptor);
+    const vertexBuffer0ArrayBuffer = await vertexBuffer0.mapWriteAsync();
+    const vertexBuffer0Float32Array = new Float32Array(vertexBuffer0ArrayBuffer);
+    vertexBuffer0Float32Array[0] = -0.5;
+    vertexBuffer0Float32Array[1] = -0.5;
+    vertexBuffer0Float32Array[2] = 1.0;
+    vertexBuffer0Float32Array[3] = 1.0;
+    vertexBuffer0Float32Array[4] = 1.0;
+
+    vertexBuffer0Float32Array[5] = -0.5;
+    vertexBuffer0Float32Array[6] = 0.5;
+    vertexBuffer0Float32Array[7] = 1.0;
+    vertexBuffer0Float32Array[8] = 1.0;
+    vertexBuffer0Float32Array[9] = 1.0;
+
+    vertexBuffer0Float32Array[10] = 0.5;
+    vertexBuffer0Float32Array[11] = -0.5;
+    vertexBuffer0Float32Array[12] = 1.0;
+    vertexBuffer0Float32Array[13] = 1.0;
+    vertexBuffer0Float32Array[14] = 1.0;
+
+    vertexBuffer0Float32Array[15] = 0.5;
+    vertexBuffer0Float32Array[16] = 0.5;
+    vertexBuffer0Float32Array[17] = 1.0;
+    vertexBuffer0Float32Array[18] = 1.0;
+    vertexBuffer0Float32Array[19] = 1.0;
+    vertexBuffer0.unmap();
+
+    const resourceBufferDescriptor = {size: Float32Array.BYTES_PER_ELEMENT, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.MAP_WRITE};
+    const resourceBuffer = device.createBuffer(resourceBufferDescriptor);
+    const resourceBufferArrayBuffer = await resourceBuffer.mapWriteAsync();
+    const resourceBufferFloat32Array = new Float32Array(resourceBufferArrayBuffer);
+    resourceBufferFloat32Array[0] = 1;
+    resourceBuffer.unmap();
+
+    const bufferBinding = {buffer: resourceBuffer, size: 4};
+    const bindGroupBinding = {binding: 0, resource: bufferBinding};
+    const bindGroupDescriptor = {layout: bindGroupLayout, bindings: [bindGroupBinding]};
+    const bindGroup = device.createBindGroup(bindGroupDescriptor);
+
+    const canvas = document.getElementById("canvas");
+    const context = canvas.getContext("gpu");
+    const swapChainDescriptor = {device, format: "bgra8unorm"};
+    const swapChain = context.configureSwapChain(swapChainDescriptor);
+    const outputTexture = swapChain.getCurrentTexture();
+    const outputTextureView = outputTexture.createDefaultView();
+
+    const commandEncoder = device.createCommandEncoder(); // {}
+    const red = {r: 0, g: 0, b: 1, a: 1};
+    const colorAttachments = [{attachment: outputTextureView, resolveTarget: null, loadOp: "clear", storeOp: "store", clearColor: red}];
+    const depthStencilAttachment = null;
+    const renderPassDescriptor = {colorAttachments, depthStencilAttachment};
+    const renderPassEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
+    renderPassEncoder.setPipeline(renderPipeline);
+    renderPassEncoder.setBindGroup(0, bindGroup);
+    renderPassEncoder.setVertexBuffers(0, [vertexBuffer0], [0]);
+    renderPassEncoder.draw(4, 1, 0, 0);
+    renderPassEncoder.endPass();
+    const commandBuffer = commandEncoder.finish();
+    device.getQueue().submit([commandBuffer]);
+
+    if (window.testRunner)
+        testRunner.notifyDone();
+}
+if (window.testRunner)
+    testRunner.waitUntilDone();
+window.addEventListener("load", start);
+</script>
+</body>
+</html>
index 7ff1ec0..d718649 100644 (file)
@@ -1,3 +1,25 @@
+2019-05-24  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [WHLSL] Allow vertex attributes to have arbitrary names in the shader
+        https://bugs.webkit.org/show_bug.cgi?id=198235
+
+        Reviewed by Dean Jackson and Justin Fan.
+
+        Metal doesn't allow arbitrary vertex attribute IDs. If you try to create a vertex attribute > 16,
+        the Metal validation layer will assert. So, we need to have a mapping from whatever the WebGPU
+        API says the vertex attribute IDs should be to the internally-used vertex attribute IDs.
+
+        Test: webgpu/whlsl-arbitrary-vertex-attribute-locations.html
+
+        * Modules/webgpu/WHLSL/Metal/WHLSLEntryPointScaffolding.cpp:
+        (WebCore::WHLSL::Metal::VertexEntryPointScaffolding::VertexEntryPointScaffolding):
+        * Modules/webgpu/WHLSL/WHLSLPipelineDescriptor.h:
+        * Modules/webgpu/WHLSL/WHLSLSemanticMatcher.cpp:
+        (WebCore::WHLSL::matchVertexAttributes):
+        * platform/graphics/gpu/cocoa/GPURenderPipelineMetal.mm:
+        (WebCore::convertRenderPipelineDescriptor):
+        (WebCore::trySetInputStateForPipelineDescriptor):
+
 2019-05-24  Timothy Hatcher  <timothy@apple.com>
 
         Refactor how showLetterpressedGlyphsWithAdvances gets the graphics context.
index 549d5dc..21974eb 100644 (file)
@@ -283,7 +283,7 @@ VertexEntryPointScaffolding::VertexEntryPointScaffolding(AST::FunctionDefinition
         NamedStageIn namedStageIn;
         namedStageIn.indexInEntryPointItems = keyValuePair.value;
         namedStageIn.elementName = m_typeNamer.generateNextStructureElementName();
-        namedStageIn.attributeIndex = keyValuePair.key->name;
+        namedStageIn.attributeIndex = keyValuePair.key->metalLocation;
         m_namedStageIns.uncheckedAppend(WTFMove(namedStageIn));
     }
 
index 5bb6665..f8f1745 100644 (file)
@@ -44,7 +44,8 @@ enum class VertexFormat : uint8_t {
 
 struct VertexAttribute {
     VertexFormat vertexFormat;
-    unsigned name;
+    unsigned shaderLocation;
+    unsigned metalLocation;
 };
 
 using VertexAttributes = Vector<VertexAttribute>;
index f614567..7a551bf 100644 (file)
@@ -164,7 +164,7 @@ static Optional<HashMap<VertexAttribute*, size_t>> matchVertexAttributes(Vector<
             if (!WTF::holds_alternative<AST::StageInOutSemantic>(semantic))
                 continue;
             auto& stageInOutSemantic = WTF::get<AST::StageInOutSemantic>(semantic);
-            if (stageInOutSemantic.index() != vertexAttribute.name)
+            if (stageInOutSemantic.index() != vertexAttribute.shaderLocation)
                 continue;
             if (!isAcceptableFormat(vertexAttribute.vertexFormat, *item.unnamedType, intrinsics))
                 return WTF::nullopt;
index e0292b8..9c4b2a0 100644 (file)
@@ -181,7 +181,7 @@ static Optional<WHLSL::RenderPipelineDescriptor> convertRenderPipelineDescriptor
         return WTF::nullopt;
 
     for (size_t i = 0; i < descriptor.inputState.attributes.size(); ++i)
-        whlslDescriptor.vertexAttributes.append({ convertVertexFormat(descriptor.inputState.attributes[i].format), static_cast<unsigned>(i) });
+        whlslDescriptor.vertexAttributes.append({ convertVertexFormat(descriptor.inputState.attributes[i].format), descriptor.inputState.attributes[i].shaderLocation, static_cast<unsigned>(i) });
 
     for (size_t i = 0; i < descriptor.colorStates.size(); ++i) {
         if (auto format = convertTextureFormat(descriptor.colorStates[i].format))
@@ -337,7 +337,7 @@ static bool trySetInputStateForPipelineDescriptor(const char* const functionName
     auto attributeArray = retainPtr(mtlVertexDescriptor.get().attributes);
 
     for (size_t i = 0; i < attributes.size(); ++i) {
-        auto location = attributes[i].shaderLocation;
+        auto location = static_cast<unsigned>(i);
         // Maximum number of vertex attributes to be supported by Web GPU.
         if (location >= 16) {
             LOG(WebGPU, "%s: Invalid shaderLocation %u for vertex attribute!", functionName, location);