|
| 1 | +// Copyright 2025 The Dawn & Tint Authors |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +// list of conditions and the following disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// 3. Neither the name of the copyright holder nor the names of its |
| 14 | +// contributors may be used to endorse or promote products derived from |
| 15 | +// this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +#include "src/dawn/native/webgpu/CaptureContext.h" |
| 29 | + |
| 30 | +#include <concepts> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +#include "dawn/common/StringViewUtils.h" |
| 34 | +#include "dawn/native/CommandBuffer.h" |
| 35 | +#include "dawn/native/Commands.h" |
| 36 | +#include "dawn/native/Device.h" |
| 37 | +#include "dawn/native/ObjectBase.h" |
| 38 | +#include "dawn/native/webgpu/BufferWGPU.h" |
| 39 | +#include "dawn/native/webgpu/Forward.h" |
| 40 | +#include "dawn/native/webgpu/QueueWGPU.h" |
| 41 | +#include "dawn/native/webgpu/Serialization.h" |
| 42 | + |
| 43 | +namespace dawn::native::webgpu { |
| 44 | + |
| 45 | +constexpr uint64_t kCopyBufferSize = 1024 * 1024; |
| 46 | + |
| 47 | +void AddReferenced(CaptureContext& captureContext, const BufferBase* buffer) {} |
| 48 | + |
| 49 | +void CaptureContext::CaptureCreation(schema::ObjectId id, |
| 50 | + const std::string& label, |
| 51 | + const RecordableObject* object) { |
| 52 | + schema::CreateResourceCmd cmd{{ |
| 53 | + .data = {{ |
| 54 | + .resource = {{ |
| 55 | + .type = object->GetObjectType(), |
| 56 | + .id = id, |
| 57 | + .label = label, |
| 58 | + }}, |
| 59 | + }}, |
| 60 | + }}; |
| 61 | + Serialize(*this, cmd); |
| 62 | + object->CaptureCreationParameters(*this); |
| 63 | +} |
| 64 | + |
| 65 | +CaptureContext::CaptureContext(Device* device, |
| 66 | + std::ostream& commandStream, |
| 67 | + std::ostream& contentStream) |
| 68 | + : mDevice(device), mCommandStream(commandStream), mContentStream(contentStream) {} |
| 69 | + |
| 70 | +CaptureContext::~CaptureContext() { |
| 71 | + if (mCopyBuffer) { |
| 72 | + mDevice->wgpu.bufferDestroy(mCopyBuffer); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +WGPUBuffer CaptureContext::GetCopyBuffer() { |
| 77 | + if (!mCopyBuffer) { |
| 78 | + WGPUBufferDescriptor desc = {}; |
| 79 | + desc.size = kCopyBufferSize; |
| 80 | + desc.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_MapRead; |
| 81 | + mCopyBuffer = mDevice->wgpu.deviceCreateBuffer(mDevice->GetInnerHandle(), &desc); |
| 82 | + } |
| 83 | + return mCopyBuffer; |
| 84 | +} |
| 85 | + |
| 86 | +void CaptureContext::WriteContentBytes(const void* data, size_t size) { |
| 87 | + mContentStream.write(reinterpret_cast<const char*>(data), size); |
| 88 | +} |
| 89 | + |
| 90 | +void CaptureContext::WriteCommandBytes(const void* data, size_t size) { |
| 91 | + mCommandStream.write(reinterpret_cast<const char*>(data), size); |
| 92 | + mCommandBytesWritten += size; |
| 93 | +} |
| 94 | + |
| 95 | +void CaptureContext::CaptureQueueWriteBuffer(BufferBase* buffer, |
| 96 | + uint64_t bufferOffset, |
| 97 | + const void* data, |
| 98 | + size_t size) { |
| 99 | + AddResource(buffer); |
| 100 | + schema::WriteBufferCmd cmd{{ |
| 101 | + .data = {{ |
| 102 | + .bufferId = GetId(buffer), |
| 103 | + .bufferOffset = bufferOffset, |
| 104 | + .size = size, |
| 105 | + }}, |
| 106 | + }}; |
| 107 | + |
| 108 | + Serialize(*this, cmd); |
| 109 | + WriteContentBytes(data, size); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace dawn::native::webgpu |
0 commit comments