2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-06-13 20:43:48 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
#ifndef GPU_BUFFER_H_
|
|
|
|
#define GPU_BUFFER_H_
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "js/RootingAPI.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "mozilla/webgpu/WebGPUTypes.h"
|
2021-03-25 13:19:44 +03:00
|
|
|
#include "nsTArray.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "ObjectModel.h"
|
2022-11-10 18:52:32 +03:00
|
|
|
#include "mozilla/ipc/RawShmem.h"
|
|
|
|
#include <memory>
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2024-07-03 19:40:17 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace webgpu {
|
|
|
|
struct MappedView;
|
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
// Give `nsTArray` some advice on how to handle `MappedInfo::mViews`.
|
|
|
|
//
|
|
|
|
// In the `mozilla::webgpu` namespace, `MappedInfo::mViews` is an
|
|
|
|
// `nsTArray<MappedView>`, and `MappedView::mArrayBuffer` is a `JS::Heap`
|
|
|
|
// pointer. This arrangement requires special handling.
|
|
|
|
//
|
|
|
|
// Normally, `nsTArray` wants its element type to be movable with simple byte
|
|
|
|
// copies, so that an `nsTArray` can efficiently resize its element buffer.
|
|
|
|
// However, `JS::Heap` is marked `MOZ_NON_MEMMOVABLE`, meaning that it cannot be
|
|
|
|
// safely moved by a simple byte-by-byte copy. Normally, this would cause
|
|
|
|
// `nsTArray` to reject `JS::Heap` as an element type, but `nsTArray.h`
|
|
|
|
// specializes `nsTArray_RelocationStrategy` to indicate that `JS::Heap` can be
|
|
|
|
// moved safely using its move constructor. This causes `nsTArray<JS::Heap<T>>`
|
|
|
|
// to perform element buffer moves using element-by-element move constructor
|
|
|
|
// application: slower, but safe for `JS::Heap`.
|
|
|
|
//
|
|
|
|
// However, while `MappedView` is automatically marked `MOZ_NON_MEMMOVABLE`
|
|
|
|
// because of its `mArrayBuffer` member, the `nsTArray_RelocationStrategy`
|
|
|
|
// specialization is not somehow similarly magically carried over from
|
|
|
|
// `JS::Heap` to `MappedView`. To use `MappedView` in `nsTArray`, we must spell
|
|
|
|
// out a relocation strategy for it.
|
|
|
|
template <>
|
|
|
|
struct nsTArray_RelocationStrategy<mozilla::webgpu::MappedView> {
|
|
|
|
// The default move constructors are fine for MappedView.
|
|
|
|
using Type =
|
|
|
|
nsTArray_RelocateUsingMoveConstructor<mozilla::webgpu::MappedView>;
|
|
|
|
};
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
namespace mozilla {
|
2021-03-25 13:19:44 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
|
|
|
namespace dom {
|
2022-08-10 18:55:05 +03:00
|
|
|
struct GPUBufferDescriptor;
|
2021-03-25 13:19:44 +03:00
|
|
|
template <typename T>
|
|
|
|
class Optional;
|
2023-05-09 20:29:56 +03:00
|
|
|
enum class GPUBufferMapState : uint8_t;
|
2022-08-10 18:55:05 +03:00
|
|
|
} // namespace dom
|
2021-03-25 13:19:44 +03:00
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
namespace webgpu {
|
|
|
|
|
|
|
|
class Device;
|
|
|
|
|
2024-07-03 19:40:17 +03:00
|
|
|
// A portion of the current mapped buffer range that is currently
|
|
|
|
// visible to JS as an ArrayBuffer.
|
|
|
|
struct MappedView {
|
|
|
|
BufferAddress mOffset;
|
|
|
|
BufferAddress mRangeEnd;
|
|
|
|
JS::Heap<JSObject*> mArrayBuffer;
|
|
|
|
|
|
|
|
MappedView(BufferAddress aOffset, BufferAddress aRangeEnd,
|
|
|
|
JSObject* aArrayBuffer)
|
|
|
|
: mOffset(aOffset), mRangeEnd(aRangeEnd), mArrayBuffer(aArrayBuffer) {}
|
|
|
|
};
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
struct MappedInfo {
|
|
|
|
// True if mapping is requested for writing.
|
|
|
|
bool mWritable = false;
|
|
|
|
// Populated by `GetMappedRange`.
|
2024-07-03 19:40:17 +03:00
|
|
|
nsTArray<MappedView> mViews;
|
2022-08-10 18:55:05 +03:00
|
|
|
BufferAddress mOffset;
|
|
|
|
BufferAddress mSize;
|
2020-10-19 20:19:12 +03:00
|
|
|
MappedInfo() = default;
|
|
|
|
MappedInfo(const MappedInfo&) = delete;
|
|
|
|
};
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
class Buffer final : public ObjectBase, public ChildOf<Device> {
|
2018-06-13 20:43:48 +03:00
|
|
|
public:
|
2022-09-07 14:22:51 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(Buffer)
|
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Buffer)
|
2019-10-02 19:46:03 +03:00
|
|
|
GPU_DECL_JS_WRAP(Buffer)
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2022-08-10 18:55:05 +03:00
|
|
|
static already_AddRefed<Buffer> Create(Device* aDevice, RawId aDeviceId,
|
|
|
|
const dom::GPUBufferDescriptor& aDesc,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2022-08-10 18:55:11 +03:00
|
|
|
already_AddRefed<dom::Promise> MapAsync(uint32_t aMode, uint64_t aOffset,
|
|
|
|
const dom::Optional<uint64_t>& aSize,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
void GetMappedRange(JSContext* aCx, uint64_t aOffset,
|
|
|
|
const dom::Optional<uint64_t>& aSize,
|
|
|
|
JS::Rooted<JSObject*>* aObject, ErrorResult& aRv);
|
|
|
|
void Unmap(JSContext* aCx, ErrorResult& aRv);
|
|
|
|
void Destroy(JSContext* aCx, ErrorResult& aRv);
|
2019-12-10 20:07:18 +03:00
|
|
|
|
2020-01-24 19:27:09 +03:00
|
|
|
const RawId mId;
|
|
|
|
|
2023-05-09 20:29:56 +03:00
|
|
|
uint64_t Size() const { return mSize; }
|
|
|
|
uint32_t Usage() const { return mUsage; }
|
|
|
|
dom::GPUBufferMapState MapState() const;
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
private:
|
2022-08-10 18:55:05 +03:00
|
|
|
Buffer(Device* const aParent, RawId aId, BufferAddress aSize, uint32_t aUsage,
|
2022-11-10 18:52:32 +03:00
|
|
|
ipc::WritableSharedMemoryMapping&& aShmem);
|
2018-06-13 20:43:48 +03:00
|
|
|
virtual ~Buffer();
|
2022-08-10 18:55:07 +03:00
|
|
|
Device& GetDevice() { return *mParent; }
|
2024-04-05 10:42:05 +03:00
|
|
|
void Cleanup();
|
2022-05-17 17:14:12 +03:00
|
|
|
void UnmapArrayBuffers(JSContext* aCx, ErrorResult& aRv);
|
2022-08-10 18:55:05 +03:00
|
|
|
void RejectMapRequest(dom::Promise* aPromise, nsACString& message);
|
|
|
|
void AbortMapRequest();
|
2022-08-10 18:55:11 +03:00
|
|
|
void SetMapped(BufferAddress aOffset, BufferAddress aSize, bool aWritable);
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
// Note: we can't map a buffer with the size that don't fit into `size_t`
|
|
|
|
// (which may be smaller than `BufferAddress`), but general not all buffers
|
|
|
|
// are mapped.
|
|
|
|
const BufferAddress mSize;
|
2022-06-29 19:43:19 +03:00
|
|
|
const uint32_t mUsage;
|
2019-12-10 20:07:18 +03:00
|
|
|
nsString mLabel;
|
2020-10-19 20:19:12 +03:00
|
|
|
// Information about the currently active mapping.
|
|
|
|
Maybe<MappedInfo> mMapped;
|
2022-08-10 18:55:05 +03:00
|
|
|
RefPtr<dom::Promise> mMapRequest;
|
2024-07-03 19:40:17 +03:00
|
|
|
|
|
|
|
// A shared memory mapping for the entire buffer, or a zero-length
|
|
|
|
// mapping.
|
|
|
|
//
|
|
|
|
// If `mUsage` contains `MAP_READ` or `MAP_WRITE`, this mapping is
|
|
|
|
// created at `Buffer` construction, and destroyed at `Buffer`
|
|
|
|
// destruction.
|
|
|
|
//
|
|
|
|
// If `mUsage` contains neither of those flags, but `this` is mapped
|
|
|
|
// at creation, this mapping is created at `Buffer` construction,
|
|
|
|
// and destroyed when we first unmap the buffer, by clearing this
|
|
|
|
// `shared_ptr`.
|
|
|
|
//
|
|
|
|
// Otherwise, this points to `WritableSharedMemoryMapping()` (the
|
|
|
|
// default constructor), a zero-length mapping that doesn't point to
|
|
|
|
// any shared memory.
|
2022-11-10 18:52:32 +03:00
|
|
|
std::shared_ptr<ipc::WritableSharedMemoryMapping> mShmem;
|
2018-06-13 20:43:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
#endif // GPU_BUFFER_H_
|