summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2025-02-20 18:59:49 -0600
committerGitHub <noreply@github.com>2025-02-20 16:59:49 -0800
commit4d286aab2ec23c081f23846f5dfdb30b1c05728b (patch)
tree98be13f2d72d90d69901d463a94c292d562cee67
parent19867ffca6dca7995c799354081219c9e76f13d1 (diff)
Metal fix (#6413)
Partially fix #6378 * Fix invalid access mode for texture_buffer * Fix texture view create issue in metal In newTextureView, levelRange should represent the mipmap level range, while sliceRange should represent the texture layer range for texture array. But the implement inverse those two wrongly.
-rw-r--r--source/slang/slang-emit-metal.cpp11
-rw-r--r--tests/metal/test_buffer.slang17
-rw-r--r--tools/gfx/metal/metal-device.cpp4
3 files changed, 28 insertions, 4 deletions
diff --git a/source/slang/slang-emit-metal.cpp b/source/slang/slang-emit-metal.cpp
index 166f02535..0a7db8b28 100644
--- a/source/slang/slang-emit-metal.cpp
+++ b/source/slang/slang-emit-metal.cpp
@@ -136,8 +136,15 @@ void MetalSourceEmitter::_emitHLSLTextureType(IRTextureTypeBase* texType)
switch (texType->getAccess())
{
case SLANG_RESOURCE_ACCESS_READ:
- m_writer->emit("access::sample");
- break;
+ {
+ // Metal does not support access::sample for texture buffers, so we need to emit
+ // access::read instead.
+ if (texType->GetBaseShape() == SLANG_TEXTURE_BUFFER)
+ m_writer->emit("access::read");
+ else
+ m_writer->emit("access::sample");
+ break;
+ }
case SLANG_RESOURCE_ACCESS_WRITE:
m_writer->emit("access::write");
diff --git a/tests/metal/test_buffer.slang b/tests/metal/test_buffer.slang
new file mode 100644
index 000000000..122af3b9a
--- /dev/null
+++ b/tests/metal/test_buffer.slang
@@ -0,0 +1,17 @@
+// Test that Buffer<T> maps to texture_buffer<uint, access::read> in Metal
+
+//TEST:SIMPLE(filecheck=METAL): -stage compute -entry computeMain -target metal
+
+
+// METAL: texture_buffer<uint, access::read> inputBuffer_{{.*}}
+Buffer<uint> inputBuffer;
+
+RWStructuredBuffer<uint> outputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dtid : SV_DispatchThreadID)
+{
+ uint idx = dtid.x;
+ // Load values from the buffer to verify correct access
+ outputBuffer[idx] = inputBuffer.Load(idx);
+}
diff --git a/tools/gfx/metal/metal-device.cpp b/tools/gfx/metal/metal-device.cpp
index e20d3ea3b..34b00d0dd 100644
--- a/tools/gfx/metal/metal-device.cpp
+++ b/tools/gfx/metal/metal-device.cpp
@@ -653,8 +653,8 @@ Result DeviceImpl::createTextureView(
MTL::PixelFormat pixelFormat = desc.format == Format::Unknown
? textureImpl->m_pixelFormat
: MetalUtil::translatePixelFormat(desc.format);
- NS::Range levelRange(sr.baseArrayLayer, sr.layerCount);
- NS::Range sliceRange(sr.mipLevel, sr.mipLevelCount);
+ NS::Range sliceRange(sr.baseArrayLayer, sr.layerCount);
+ NS::Range levelRange(sr.mipLevel, sr.mipLevelCount);
viewImpl->m_textureView = NS::TransferPtr(textureImpl->m_texture->newTextureView(
pixelFormat,