Skip to content

Commit 4d286aa

Browse files
authored
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.
1 parent 19867ff commit 4d286aa

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

source/slang/slang-emit-metal.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,15 @@ void MetalSourceEmitter::_emitHLSLTextureType(IRTextureTypeBase* texType)
136136
switch (texType->getAccess())
137137
{
138138
case SLANG_RESOURCE_ACCESS_READ:
139-
m_writer->emit("access::sample");
140-
break;
139+
{
140+
// Metal does not support access::sample for texture buffers, so we need to emit
141+
// access::read instead.
142+
if (texType->GetBaseShape() == SLANG_TEXTURE_BUFFER)
143+
m_writer->emit("access::read");
144+
else
145+
m_writer->emit("access::sample");
146+
break;
147+
}
141148

142149
case SLANG_RESOURCE_ACCESS_WRITE:
143150
m_writer->emit("access::write");

tests/metal/test_buffer.slang

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test that Buffer<T> maps to texture_buffer<uint, access::read> in Metal
2+
3+
//TEST:SIMPLE(filecheck=METAL): -stage compute -entry computeMain -target metal
4+
5+
6+
// METAL: texture_buffer<uint, access::read> inputBuffer_{{.*}}
7+
Buffer<uint> inputBuffer;
8+
9+
RWStructuredBuffer<uint> outputBuffer;
10+
11+
[numthreads(4, 1, 1)]
12+
void computeMain(uint3 dtid : SV_DispatchThreadID)
13+
{
14+
uint idx = dtid.x;
15+
// Load values from the buffer to verify correct access
16+
outputBuffer[idx] = inputBuffer.Load(idx);
17+
}

tools/gfx/metal/metal-device.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,8 @@ Result DeviceImpl::createTextureView(
653653
MTL::PixelFormat pixelFormat = desc.format == Format::Unknown
654654
? textureImpl->m_pixelFormat
655655
: MetalUtil::translatePixelFormat(desc.format);
656-
NS::Range levelRange(sr.baseArrayLayer, sr.layerCount);
657-
NS::Range sliceRange(sr.mipLevel, sr.mipLevelCount);
656+
NS::Range sliceRange(sr.baseArrayLayer, sr.layerCount);
657+
NS::Range levelRange(sr.mipLevel, sr.mipLevelCount);
658658

659659
viewImpl->m_textureView = NS::TransferPtr(textureImpl->m_texture->newTextureView(
660660
pixelFormat,

0 commit comments

Comments
 (0)