[Web GPU] Buffer updates part 2: setSubData, GPU/CPU synchronization
authorjustin_fan@apple.com <justin_fan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Feb 2019 21:10:24 +0000 (21:10 +0000)
committerjustin_fan@apple.com <justin_fan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 Feb 2019 21:10:24 +0000 (21:10 +0000)
commitf1fabac889faeef4de9bf850b109b90a0f81326c
treeff29628470bc127fedafafda1450d9a62c2bd570
parent5dd975337acc1b445d945b4e82f70dff529d1324
[Web GPU] Buffer updates part 2: setSubData, GPU/CPU synchronization
https://bugs.webkit.org/show_bug.cgi?id=195077
<rdar://problem/47805229>

Reviewed by Dean Jackson.

Source/WebCore:

Implement GPUBuffer.setSubData and prevent the resolving of mapping promises if the buffer is scheduled to be
used on the GPU, and add handlers to resolve such promises after command buffer execution completes. In addition,
update buffer sizes to u64 (unsigned long in C++) as per the updated Web GPU API.

Tests: webgpu/buffer-command-buffer-races.html
       webgpu/map-read-buffers.html

* Modules/webgpu/WebGPUBindGroup.h:
(WebCore::WebGPUBindGroup::bindGroup const):
* Modules/webgpu/WebGPUBindGroupDescriptor.cpp:
(WebCore::WebGPUBindGroupDescriptor::asGPUBindGroupDescriptor const): Rename binding -> bufferBinding to reduce confusion.
* Modules/webgpu/WebGPUBuffer.cpp: Small tweaks.
(WebCore::WebGPUBuffer::setSubData):
(WebCore::WebGPUBuffer::unmap): Correctly fail if buffer is destroyed.
(WebCore::WebGPUBuffer::destroy):
(WebCore::WebGPUBuffer::rejectOrRegisterPromiseCallback):
* Modules/webgpu/WebGPUBuffer.h:
(WebCore::WebGPUBuffer::buffer const): Returned buffer is no longer const so that it can be used in completed handler callbacks.
* Modules/webgpu/WebGPUBuffer.idl: Enable setSubData.
* Modules/webgpu/WebGPUCommandBuffer.cpp:
(WebCore::WebGPUCommandBuffer::beginRenderPass):
* Modules/webgpu/WebGPUProgrammablePassEncoder.cpp:
(WebCore::WebGPUProgrammablePassEncoder::setBindGroup const):
* Modules/webgpu/WebGPUProgrammablePassEncoder.h:
* Modules/webgpu/WebGPURenderPassEncoder.cpp:
(WebCore::WebGPURenderPassEncoder::setVertexBuffers):
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* platform/graphics/gpu/GPUBuffer.h:
(WebCore::GPUBuffer::isTransferDst const): Added various state and flag getters.
(WebCore::GPUBuffer::isVertex const):
(WebCore::GPUBuffer::isUniform const):
(WebCore::GPUBuffer::isStorage const):
(WebCore::GPUBuffer::isReadOnly const):
(WebCore::GPUBuffer::isMappable const):
(WebCore::GPUBuffer::isMapWrite const):
(WebCore::GPUBuffer::isMapRead const):
(WebCore::GPUBuffer::isMapWriteable const):
(WebCore::GPUBuffer::isMapReadable const):
* platform/graphics/gpu/GPUBufferBinding.h:
* platform/graphics/gpu/GPUCommandBuffer.h:
(WebCore::GPUCommandBuffer::usedBuffers const):
(WebCore::GPUCommandBuffer::useBuffer):
* platform/graphics/gpu/GPUDevice.cpp:
(WebCore::GPUDevice::tryCreateBuffer): Pass Ref of itself for Buffer to request the Queue later, if needed.
(WebCore::GPUDevice::tryCreateBuffer const): Deleted.
* platform/graphics/gpu/GPUDevice.h:
* platform/graphics/gpu/GPUProgrammablePassEncoder.cpp: Retain the encoder's commandBuffer to reference its used resource buffers.
(WebCore::GPUProgrammablePassEncoder::GPUProgrammablePassEncoder):
* platform/graphics/gpu/GPUProgrammablePassEncoder.h:
(WebCore::GPUProgrammablePassEncoder::commandBuffer const):
* platform/graphics/gpu/GPURenderPassEncoder.h:
* platform/graphics/gpu/cocoa/GPUBufferMetal.mm:
(WebCore::GPUBuffer::validateBufferCreate): Move validation out of tryCreate.
(WebCore::GPUBuffer::tryCreate): Create both shared and private buffers, depending on usage.
(WebCore::GPUBuffer::GPUBuffer):
(WebCore::GPUBuffer::~GPUBuffer): Call destroy instead of just unmap.
(WebCore::GPUBuffer::state const):
(WebCore::GPUBuffer::setSubData): Uses a cached collection of staging MTLBuffers to encode data copies to the implementation MTLBuffer.
(WebCore::GPUBuffer::commandBufferCommitted): Register on the GPUBuffer that the provided MTLCommandBuffer uses it, and is about to be committed.
(WebCore::GPUBuffer::commandBufferCompleted): MTLCommandBuffer's onCompletedHandler calls this.
(WebCore::GPUBuffer::reuseSubDataBuffer): SetSubData's blit command buffers call this to return a staging buffer to the pool.
(WebCore::GPUBuffer::registerMappingCallback):
(WebCore::GPUBuffer::runMappingCallback):
(WebCore::GPUBuffer::unmap):
(WebCore::GPUBuffer::destroy):
(WebCore::GPUBuffer::tryCreateSharedBuffer): Deleted.
* platform/graphics/gpu/cocoa/GPUProgrammablePassEncoderMetal.mm:
(WebCore::GPUProgrammablePassEncoder::setResourceAsBufferOnEncoder):
(WebCore::GPUProgrammablePassEncoder::setBindGroup):
* platform/graphics/gpu/cocoa/GPUQueueMetal.mm:
(WebCore::GPUQueue::submit): Ensure submitted buffers are in the correct state, and add completed handlers for synchronization.
* platform/graphics/gpu/cocoa/GPURenderPassEncoderMetal.mm:
(WebCore::GPURenderPassEncoder::tryCreate):
(WebCore::GPURenderPassEncoder::GPURenderPassEncoder): Retain the commandBuffer for later reference.
(WebCore::GPURenderPassEncoder::setVertexBuffers): Mark vertex buffers as used before submission.
(WebCore::GPURenderPassEncoder::create): Deleted.

Buffer size updates in the IDL:
* Modules/webgpu/GPUBufferDescriptor.idl:
* Modules/webgpu/WebGPUBuffer.idl:
* Modules/webgpu/WebGPUBufferBinding.idl:
* Modules/webgpu/WebGPUCommandBuffer.idl:
* Modules/webgpu/WebGPURenderPassEncoder.idl:
* Modules/webgpu/WebGPUVertexAttributeDescriptor.idl:
* Modules/webgpu/WebGPUVertexInputDescriptor.idl:

LayoutTests:

Add tests for mapReadAysnc and setSubData calls. Nofity testRunner when done on some drawing tests
that may take more time.

* webgpu/buffer-command-buffer-races-expected.html: Added.
* webgpu/buffer-command-buffer-races.html: Added.
* webgpu/buffer-resource-triangles.html: Use setSubData.
* webgpu/depth-enabled-triangle-strip.html: Ditto.
* webgpu/map-read-buffers-expected.txt: Added.
* webgpu/map-read-buffers.html: Added.
* webgpu/vertex-buffer-triangle-strip.html: Use setSubData.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242148 268f45cc-cd09-0410-ab3c-d52691b4dbfc
38 files changed:
LayoutTests/ChangeLog
LayoutTests/webgpu/buffer-command-buffer-races-expected.html [new file with mode: 0644]
LayoutTests/webgpu/buffer-command-buffer-races.html [new file with mode: 0644]
LayoutTests/webgpu/buffer-resource-triangles.html
LayoutTests/webgpu/depth-enabled-triangle-strip.html
LayoutTests/webgpu/map-read-buffers-expected.txt [new file with mode: 0644]
LayoutTests/webgpu/map-read-buffers.html [new file with mode: 0644]
LayoutTests/webgpu/vertex-buffer-triangle-strip.html
Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/GPUBufferDescriptor.idl
Source/WebCore/Modules/webgpu/WebGPUBindGroup.h
Source/WebCore/Modules/webgpu/WebGPUBindGroupDescriptor.cpp
Source/WebCore/Modules/webgpu/WebGPUBuffer.cpp
Source/WebCore/Modules/webgpu/WebGPUBuffer.h
Source/WebCore/Modules/webgpu/WebGPUBuffer.idl
Source/WebCore/Modules/webgpu/WebGPUBufferBinding.idl
Source/WebCore/Modules/webgpu/WebGPUCommandBuffer.cpp
Source/WebCore/Modules/webgpu/WebGPUCommandBuffer.idl
Source/WebCore/Modules/webgpu/WebGPUProgrammablePassEncoder.cpp
Source/WebCore/Modules/webgpu/WebGPUProgrammablePassEncoder.h
Source/WebCore/Modules/webgpu/WebGPURenderPassEncoder.cpp
Source/WebCore/Modules/webgpu/WebGPURenderPassEncoder.idl
Source/WebCore/Modules/webgpu/WebGPUVertexAttributeDescriptor.idl
Source/WebCore/Modules/webgpu/WebGPUVertexInputDescriptor.idl
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/graphics/gpu/GPUBuffer.h
Source/WebCore/platform/graphics/gpu/GPUBufferBinding.h
Source/WebCore/platform/graphics/gpu/GPUCommandBuffer.h
Source/WebCore/platform/graphics/gpu/GPUDevice.cpp
Source/WebCore/platform/graphics/gpu/GPUDevice.h
Source/WebCore/platform/graphics/gpu/GPUProgrammablePassEncoder.cpp [new file with mode: 0644]
Source/WebCore/platform/graphics/gpu/GPUProgrammablePassEncoder.h
Source/WebCore/platform/graphics/gpu/GPURenderPassEncoder.h
Source/WebCore/platform/graphics/gpu/cocoa/GPUBufferMetal.mm
Source/WebCore/platform/graphics/gpu/cocoa/GPUProgrammablePassEncoderMetal.mm
Source/WebCore/platform/graphics/gpu/cocoa/GPUQueueMetal.mm
Source/WebCore/platform/graphics/gpu/cocoa/GPURenderPassEncoderMetal.mm