Browse Source

metal: texel buffers align their size according to device requirements

Sasha Szpakowski 1 year ago
parent
commit
af840bb2cf
1 changed files with 15 additions and 2 deletions
  1. 15 2
      src/modules/graphics/metal/Buffer.mm

+ 15 - 2
src/modules/graphics/metal/Buffer.mm

@@ -21,6 +21,8 @@
 #include "Buffer.h"
 #include "Buffer.h"
 #include "Graphics.h"
 #include "Graphics.h"
 
 
+#include "common/memory.h"
+
 namespace love
 namespace love
 {
 {
 namespace graphics
 namespace graphics
@@ -65,6 +67,19 @@ Buffer::Buffer(love::graphics::Graphics *gfx, id<MTLDevice> device, const Settin
 	size = getSize();
 	size = getSize();
 	arraylength = getArrayLength();
 	arraylength = getArrayLength();
 
 
+	if (usageFlags & BUFFERUSAGEFLAG_TEXEL)
+	{
+		if (@available(iOS 12, macOS 10.14, *))
+		{
+			MTLPixelFormat pixformat = getMTLPixelFormat(getDataMember(0).decl.format);
+			NSUInteger alignment = 1;
+			if (pixformat != MTLPixelFormatInvalid)
+				alignment = [device minimumTextureBufferAlignmentForPixelFormat:pixformat];
+
+			size = alignUp(size, (size_t) alignment);
+		}
+	}
+
 	MTLResourceOptions opts = 0;
 	MTLResourceOptions opts = 0;
 	if (settings.dataUsage == BUFFERDATAUSAGE_READBACK)
 	if (settings.dataUsage == BUFFERDATAUSAGE_READBACK)
 		opts |= MTLResourceStorageModeShared;
 		opts |= MTLResourceStorageModeShared;
@@ -83,8 +98,6 @@ Buffer::Buffer(love::graphics::Graphics *gfx, id<MTLDevice> device, const Settin
 	{
 	{
 		if (@available(iOS 12, macOS 10.14, *))
 		if (@available(iOS 12, macOS 10.14, *))
 		{
 		{
-			// TODO: minimumTextureBufferAlignmentForPixelFormat
-
 			MTLPixelFormat pixformat = getMTLPixelFormat(getDataMember(0).decl.format);
 			MTLPixelFormat pixformat = getMTLPixelFormat(getDataMember(0).decl.format);
 			if (pixformat == MTLPixelFormatInvalid)
 			if (pixformat == MTLPixelFormatInvalid)
 				throw love::Exception("Could not create Metal texel buffer: invalid format.");
 				throw love::Exception("Could not create Metal texel buffer: invalid format.");