Skip to content

Commit 0dd9076

Browse files
Add bgra8 format (#6163)
* add brga8 format * add tests * minor fixes * cleanup * maybe fix broken quad control test * add missing xslang flag on test --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent 92c9fff commit 0dd9076

9 files changed

+89
-6
lines changed

include/slang-image-format-defs.h

+1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ SLANG_FORMAT(r16ui, (UINT16, 1, sizeof(uint16_t)))
4545
SLANG_FORMAT(r8ui, (UINT8, 1, sizeof(uint8_t)))
4646
SLANG_FORMAT(r64ui, (UINT64, 1, sizeof(uint64_t)))
4747
SLANG_FORMAT(r64i, (INT64, 1, sizeof(int64_t)))
48+
SLANG_FORMAT(bgra8, (UINT8, 4, sizeof(uint32_t)))
4849

4950
#undef SLANG_FORMAT

source/slang/slang-diagnostic-defs.h

+7
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,13 @@ DIAGNOSTIC(
10591059
explicitUniformLocation,
10601060
"Explicit binding of uniform locations is discouraged. Prefer 'ConstantBuffer<$0>' over "
10611061
"'uniform $0'")
1062+
DIAGNOSTIC(
1063+
31105,
1064+
Warning,
1065+
imageFormatUnsupportedByBackend,
1066+
"Image format '$0' is not explicitly supported by the $1 backend, using supported format '$2' "
1067+
"instead.")
1068+
10621069

10631070
DIAGNOSTIC(31120, Error, invalidAttributeTarget, "invalid syntax target for user defined attribute")
10641071

source/slang/slang-emit-glsl.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,19 @@ void GLSLSourceEmitter::_emitGLSLParameterGroup(
608608
m_writer->emit(";\n");
609609
}
610610

611+
static bool isImageFormatSupportedByGLSL(ImageFormat format)
612+
{
613+
switch (format)
614+
{
615+
case ImageFormat::bgra8:
616+
// These are formats Slang accept, but are not explicitly supported in GLSL.
617+
return false;
618+
default:
619+
return true;
620+
}
621+
};
622+
623+
611624
void GLSLSourceEmitter::_emitGLSLImageFormatModifier(IRInst* var, IRTextureType* resourceType)
612625
{
613626
SLANG_UNUSED(resourceType);
@@ -618,6 +631,18 @@ void GLSLSourceEmitter::_emitGLSLImageFormatModifier(IRInst* var, IRTextureType*
618631
if (auto formatDecoration = var->findDecoration<IRFormatDecoration>())
619632
{
620633
auto format = formatDecoration->getFormat();
634+
const auto formatInfo = getImageFormatInfo(format);
635+
if (!isImageFormatSupportedByGLSL(format))
636+
{
637+
getSink()->diagnose(
638+
SourceLoc(),
639+
Diagnostics::imageFormatUnsupportedByBackend,
640+
formatInfo.name,
641+
"GLSL",
642+
"unknown");
643+
format = ImageFormat::unknown;
644+
}
645+
621646
if (format == ImageFormat::unknown)
622647
{
623648
// If the user explicitly opts out of having a format, then
@@ -636,7 +661,6 @@ void GLSLSourceEmitter::_emitGLSLImageFormatModifier(IRInst* var, IRTextureType*
636661
}
637662
else
638663
{
639-
auto formatInfo = getImageFormatInfo(format);
640664
if (formatInfo.scalarType == SLANG_SCALAR_TYPE_UINT64 ||
641665
formatInfo.scalarType == SLANG_SCALAR_TYPE_INT64)
642666
{

source/slang/slang-emit-spirv.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
19011901
}
19021902
}
19031903

1904-
static SpvImageFormat getSpvImageFormat(IRTextureTypeBase* type)
1904+
SpvImageFormat getSpvImageFormat(IRTextureTypeBase* type)
19051905
{
19061906
ImageFormat imageFormat =
19071907
type->hasFormat() ? (ImageFormat)type->getFormat() : ImageFormat::unknown;
@@ -1992,7 +1992,14 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
19921992
case ImageFormat::r64i:
19931993
return SpvImageFormatR64i;
19941994
default:
1995-
SLANG_UNIMPLEMENTED_X("unknown image format for spirv emit");
1995+
const auto imageFormatInfo = getImageFormatInfo(imageFormat);
1996+
m_sink->diagnose(
1997+
SourceLoc(),
1998+
Diagnostics::imageFormatUnsupportedByBackend,
1999+
imageFormatInfo.name,
2000+
"SPIRV",
2001+
"unknown");
2002+
return SpvImageFormatUnknown;
19962003
}
19972004
}
19982005

source/slang/slang-emit-wgsl.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void WGSLSourceEmitter::emit(const AddressSpace addressSpace)
326326
}
327327
}
328328

329-
static const char* getWgslImageFormat(IRTextureTypeBase* type)
329+
const char* WGSLSourceEmitter::getWgslImageFormat(IRTextureTypeBase* type)
330330
{
331331
// You can find the supported WGSL texel format from the URL:
332332
// https://www.w3.org/TR/WGSL/#storage-texel-formats
@@ -405,11 +405,19 @@ static const char* getWgslImageFormat(IRTextureTypeBase* type)
405405
return "rgba32sint";
406406
case ImageFormat::rgba32f:
407407
return "rgba32float";
408+
case ImageFormat::bgra8:
409+
return "bgra8unorm";
408410
case ImageFormat::unknown:
409411
// Unlike SPIR-V, WGSL doesn't have a texel format for "unknown".
410412
return "rgba32float";
411413
default:
412-
// We may need to print a warning for types WGSL doesn't support
414+
const auto imageFormatInfo = getImageFormatInfo(imageFormat);
415+
getSink()->diagnose(
416+
SourceLoc(),
417+
Diagnostics::imageFormatUnsupportedByBackend,
418+
imageFormatInfo.name,
419+
"WGSL",
420+
"rgba32float");
413421
return "rgba32float";
414422
}
415423
}

source/slang/slang-emit-wgsl.h

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
7171
const IRIntegerValue& rowCountWGSL,
7272
const IRIntegerValue& colCountWGSL);
7373

74+
const char* getWgslImageFormat(IRTextureTypeBase* type);
75+
7476
bool m_f16ExtensionEnabled = false;
7577
};
7678

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK_SPIRV): -target spirv
2+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl
3+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK_WGSL): -target wgsl
4+
5+
// CHECK_SPIRV: warning 31105{{.*}}bgra8
6+
// CHECK_GLSL: warning 31105{{.*}}bgra8
7+
[format("bgra8")]
8+
RWTexture2D<float4> outputTexture;
9+
10+
// CHECK_WGSL: warning 31105{{.*}}rg8
11+
[format("rg8")]
12+
RWTexture2D<float4> outputTexture2;
13+
14+
[numthreads(8, 8, 1)]
15+
void main(uint3 threadID : SV_DispatchThreadID)
16+
{
17+
outputTexture[threadID.xy] = float4(1.0, 1.0, 1.0, 1.0);
18+
outputTexture2[threadID.xy] = float4(1.0, 1.0, 1.0, 1.0);
19+
}

tests/hlsl-intrinsic/quad-control/quad-control-comp-functionality.slang

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
//TEST(compute):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -emit-spirv-directly
22
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -profile cs_6_7 -dx12 -use-dxil -shaderobj -render-feature hardware-device
3-
//TEST(compute):COMPARE_COMPUTE_EX:-metal -compute -shaderobj
3+
//TEST(compute):COMPARE_COMPUTE_EX:-metal -compute -shaderobj -xslang -DMETAL
44

55
//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name outputBuffer
66
RWStructuredBuffer<uint> outputBuffer;
77

88
[numthreads(16, 1, 1)]
99
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
1010
{
11+
#if !defined(METAL)
12+
uint index = WaveGetLaneIndex();
13+
#else
1114
uint index = dispatchThreadID.x;
15+
#endif
1216

1317
if (index < 4)
1418
{

tests/wgsl/texture-storage.slang

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ RWStructuredBuffer<int> outputBuffer;
2828
[format("rgba8")] RWTexture3D<float4> w3D_f32v4;
2929
[format("rgba8")] RWTexture2DArray<float4> w2DArray_f32v4;
3030

31+
//TEST_INPUT: RWTexture1D(format=B8G8R8A8_UNORM, size=4, content = zero):name w1D_f32_bgra_v4
32+
//TEST_INPUT: RWTexture2D(format=B8G8R8A8_UNORM, size=4, content = zero):name w2D_f32_bgra_v4
33+
//TEST_INPUT: RWTexture3D(format=B8G8R8A8_UNORM, size=4, content = zero):name w3D_f32_bgra_v4
34+
//TEST_INPUT: RWTexture2D(format=B8G8R8A8_UNORM, size=4, content = zero, arrayLength=2):name w2DArray_f32_bgra_v4
35+
// WGSL: var w1D_f32_bgra_v4{{[^:]*}}: texture_storage_1d<bgra8unorm, read_write>
36+
[format("bgra8")] RWTexture1D<float4> w1D_f32_bgra_v4;
37+
[format("bgra8")] RWTexture2D<float4> w2D_f32_bgra_v4;
38+
[format("bgra8")] RWTexture3D<float4> w3D_f32_bgra_v4;
39+
[format("bgra8")] RWTexture2DArray<float4> w2DArray_f32_bgra_v4;
40+
3141
// i32 types
3242

3343
//TEST_INPUT: RWTexture1D(format=R32G32_SINT, size=4, content = zero):name w1D_i32v2
@@ -127,6 +137,7 @@ void fragMain()
127137
bool result = true
128138
&& TEST_textureStorage_StoreLoad<float2>(w1D_f32v2, w2D_f32v2, w3D_f32v2, w2DArray_f32v2)
129139
&& TEST_textureStorage_StoreLoad<float4>(w1D_f32v4, w2D_f32v4, w3D_f32v4, w2DArray_f32v4)
140+
&& TEST_textureStorage_StoreLoad<float4>(w1D_f32_bgra_v4, w2D_f32_bgra_v4, w3D_f32_bgra_v4, w2DArray_f32_bgra_v4)
130141
&& TEST_textureStorage_StoreLoad<int32_t2>(w1D_i32v2, w2D_i32v2, w3D_i32v2, w2DArray_i32v2)
131142
&& TEST_textureStorage_StoreLoad<int32_t4>(w1D_i32v4, w2D_i32v4, w3D_i32v4, w2DArray_i32v4)
132143
&& TEST_textureStorage_StoreLoad<uint32_t2>(w1D_u32v2, w2D_u32v2, w3D_u32v2, w2DArray_u32v2)

0 commit comments

Comments
 (0)