Skip to content

Commit

Permalink
graphics: move buffer:clear error checking to high level code
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Jan 2, 2024
1 parent af840bb commit 77c5ea1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 34 deletions.
14 changes: 14 additions & 0 deletions src/modules/graphics/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ int Buffer::getDataMemberIndex(const std::string &name) const
return -1;
}

void Buffer::clear(size_t offset, size_t size)
{
if (isImmutable())
throw love::Exception("Cannot clear an immutable Buffer.");
else if (isMapped())
throw love::Exception("Cannot clear a mapped Buffer.");
else if (offset + size > getSize())
throw love::Exception("The given offset and size parameters to clear() are not within the Buffer's size.");
else if (offset % 4 != 0 || size % 4 != 0)
throw love::Exception("clear() must be used with offset and size parameters that are multiples of 4 bytes.");

clearInternal(offset, size);
}

std::vector<Buffer::DataDeclaration> Buffer::getCommonFormatDeclaration(CommonFormat format)
{
switch (format)
Expand Down
4 changes: 3 additions & 1 deletion src/modules/graphics/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Buffer : public love::Object, public Resource
/**
* Reset the given portion of this buffer's data to 0.
*/
virtual void clear(size_t offset, size_t size) = 0;
void clear(size_t offset, size_t size);

/**
* Copy a portion of this Buffer's data to another buffer, using the GPU.
Expand Down Expand Up @@ -178,6 +178,8 @@ class Buffer : public love::Object, public Resource

protected:

virtual void clearInternal(size_t offset, size_t size) = 0;

std::vector<DataMember> dataMembers;
size_t arrayLength;
size_t arrayStride;
Expand Down
3 changes: 2 additions & 1 deletion src/modules/graphics/metal/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ class Buffer final : public love::graphics::Buffer
void *map(MapType map, size_t offset, size_t size) override;
void unmap(size_t usedoffset, size_t usedsize) override;
bool fill(size_t offset, size_t size, const void *data) override;
void clear(size_t offset, size_t size) override;
void copyTo(love::graphics::Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size) override;

ptrdiff_t getHandle() const override { return (ptrdiff_t) buffer; }
ptrdiff_t getTexelBufferHandle() const override { return (ptrdiff_t) texture; }

private:

void clearInternal(size_t offset, size_t size) override;

id<MTLBuffer> buffer;
id<MTLTexture> texture;

Expand Down
11 changes: 1 addition & 10 deletions src/modules/graphics/metal/Buffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,8 @@ static MTLPixelFormat getMTLPixelFormat(DataFormat format)
return true;
}}

void Buffer::clear(size_t offset, size_t size)
void Buffer::clearInternal(size_t offset, size_t size)
{ @autoreleasepool {
if (isImmutable())
throw love::Exception("Cannot clear an immutable Buffer.");
else if (isMapped())
throw love::Exception("Cannot clear a mapped Buffer.");
else if (offset + size > getSize())
throw love::Exception("The given offset and size parameters to clear() are not within the Buffer's size.");
else if (offset % 4 != 0 || size % 4 != 0)
throw love::Exception("clear() must be used with offset and size parameters that are multiples of 4 bytes.");

auto gfx = Graphics::getInstance();
auto encoder = gfx->useBlitEncoder();

Expand Down
11 changes: 1 addition & 10 deletions src/modules/graphics/opengl/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,8 @@ bool Buffer::fill(size_t offset, size_t size, const void *data)
return true;
}

void Buffer::clear(size_t offset, size_t size)
void Buffer::clearInternal(size_t offset, size_t size)
{
if (isImmutable())
throw love::Exception("Cannot clear an immutable Buffer.");
else if (isMapped())
throw love::Exception("Cannot clear a mapped Buffer.");
else if (offset + size > getSize())
throw love::Exception("The given offset and size parameters to clear() are not within the Buffer's size.");
else if (offset % 4 != 0 || size % 4 != 0)
throw love::Exception("clear() must be used with offset and size parameters that are multiples of 4 bytes.");

if (GLAD_VERSION_4_3)
{
gl.bindBuffer(mapUsage, buffer);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/graphics/opengl/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class Buffer final : public love::graphics::Buffer, public Volatile
void *map(MapType map, size_t offset, size_t size) override;
void unmap(size_t usedoffset, size_t usedsize) override;
bool fill(size_t offset, size_t size, const void *data) override;
void clear(size_t offset, size_t size) override;
void copyTo(love::graphics::Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size) override;

ptrdiff_t getHandle() const override { return buffer; };
Expand All @@ -66,6 +65,8 @@ class Buffer final : public love::graphics::Buffer, public Volatile
bool load(const void *initialdata);
bool supportsOrphan() const;

void clearInternal(size_t offset, size_t size) override;

BufferUsage mapUsage = BUFFERUSAGE_VERTEX;
GLenum target = 0;

Expand Down
11 changes: 1 addition & 10 deletions src/modules/graphics/vulkan/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,8 @@ void Buffer::unmap(size_t usedoffset, size_t usedsize)
}
}

void Buffer::clear(size_t offset, size_t size)
void Buffer::clearInternal(size_t offset, size_t size)
{
if (isImmutable())
throw love::Exception("Cannot clear an immutable Buffer.");
else if (isMapped())
throw love::Exception("Cannot clear a mapped Buffer.");
else if (offset + size > getSize())
throw love::Exception("The given offset and size parameters to clear() are not within the Buffer's size.");
else if (offset % 4 != 0 || size % 4 != 0)
throw love::Exception("clear() must be used with offset and size parameters that are multiples of 4 bytes.");

vkCmdFillBuffer(vgfx->getCommandBufferForDataTransfer(), buffer, offset, size, 0);
}

Expand Down
4 changes: 3 additions & 1 deletion src/modules/graphics/vulkan/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ class Buffer final
void *map(MapType map, size_t offset, size_t size) override;
void unmap(size_t usedoffset, size_t usedsize) override;
bool fill(size_t offset, size_t size, const void *data) override;
void clear(size_t offset, size_t size) override;
void copyTo(love::graphics::Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size) override;
ptrdiff_t getHandle() const override;
ptrdiff_t getTexelBufferHandle() const override;

private:

void clearInternal(size_t offset, size_t size) override;

bool zeroInitialize;
const void *initialData;
VkBuffer buffer = VK_NULL_HANDLE;
Expand Down

0 comments on commit 77c5ea1

Please sign in to comment.