Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit b9ea7d3

Browse files
committed
Fix latent translation issues of GLSL built-in pack functions
The pack functions packUnorm4x8/packSnorm4x8/packUnorm2x16/packSnorm2x16 always need a rounding calculation. This is missing in the translation. Change-Id: Ib8e53debf56b94fde54225592bf85db498cc3391
1 parent e231768 commit b9ea7d3

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

llpc/test/shaderdb/OpExtInst_TestPackSnorm2x16_lit.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void main()
1919
; SHADERTEST-LABEL: {{^// LLPC}} SPIRV-to-LLVM translation results
2020
; SHADERTEST: %[[CLAMP:.*]] = call <2 x float> (...) @lgc.create.fclamp.v2f32(<2 x float> %{{.*}}, <2 x float> <float -1.000000e+00, float -1.000000e+00>, <2 x float> <float 1.000000e+00, float 1.000000e+00>)
2121
; SHADERTEST: %[[SCALE:.*]] = fmul <2 x float> %[[CLAMP]], <float 3.276700e+04, float 3.276700e+04>
22-
; SHADERTEST: %[[CONV:.*]] = fptosi <2 x float> %[[SCALE]] to <2 x i16>
22+
; SHADERTEST: %[[RINT:.*]] = call <2 x float> @llvm.rint.v2f32(<2 x float> %[[SCALE]])
23+
; SHADERTEST: %[[CONV:.*]] = fptosi <2 x float> %[[RINT]] to <2 x i16>
2324
; SHADERTEST: = bitcast <2 x i16> %[[CONV]] to i32
2425
; SHADERTEST: AMDLLPC SUCCESS
2526
*/

llpc/test/shaderdb/OpExtInst_TestPackUnorm2x16_lit.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void main()
1919
; SHADERTEST-LABEL: {{^// LLPC}} SPIRV-to-LLVM translation results
2020
; SHADERTEST: %[[CLAMP:.*]] = call <2 x float> (...) @lgc.create.fclamp.v2f32(<2 x float> %1, <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00>)
2121
; SHADERTEST: %[[SCALE:.*]] = fmul <2 x float> %[[CLAMP]], <float 6.553500e+04, float 6.553500e+04>
22-
; SHADERTEST: %[[CONV:.*]] = fptoui <2 x float> %[[SCALE]] to <2 x i16>
22+
; SHADERTEST: %[[RINT:.*]] = call <2 x float> @llvm.rint.v2f32(<2 x float> %[[SCALE]])
23+
; SHADERTEST: %[[CONV:.*]] = fptoui <2 x float> %[[RINT]] to <2 x i16>
2324
; SHADERTEST: = bitcast <2 x i16> %[[CONV]] to i32
2425
; SHADERTEST: AMDLLPC SUCCESS
2526
*/

llpc/test/shaderdb/OpExtInst_TestPackUnorm4x8_lit.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void main()
1919
; SHADERTEST-LABEL: {{^// LLPC}} SPIRV-to-LLVM translation results
2020
; SHADERTEST: %[[CLAMP:.*]] = call <4 x float> (...) @lgc.create.fclamp.v4f32(<4 x float> %{{.*}}, <4 x float> zeroinitializer, <4 x float> <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>)
2121
; SHADERTEST: %[[SCALE:.*]] = fmul <4 x float> %[[CLAMP]], <float 2.550000e+02, float 2.550000e+02, float 2.550000e+02, float 2.550000e+02>
22-
; SHADERTEST: %[[CONV:.*]] = fptoui <4 x float> %[[SCALE]] to <4 x i8>
22+
; SHADERTEST: %[[RINT:.*]] = call <4 x float> @llvm.rint.v2f32(<4 x float> %[[SCALE]])
23+
; SHADERTEST: %[[CONV:.*]] = fptoui <4 x float> %[[RINT]] to <4 x i8>
2324
; SHADERTEST: = bitcast <4 x i8> %[[CONV]] to i32
2425
; SHADERTEST: AMDLLPC SUCCESS
2526
*/

llpc/translator/lib/SPIRV/SPIRVReader.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7957,6 +7957,7 @@ Value *SPIRVToLLVM::transGLSLExtInst(SPIRVExtInst *extInst, BasicBlock *bb) {
79577957

79587958
case GLSLstd450PackSnorm4x8: {
79597959
// Convert <4 x float> into signed normalized <4 x i8> then pack into i32.
7960+
// round(clamp(c, -1, +1) * 127.0)
79607961
Value *val = getBuilder()->CreateFClamp(args[0], ConstantFP::get(args[0]->getType(), -1.0),
79617962
ConstantFP::get(args[0]->getType(), 1.0));
79627963
val = getBuilder()->CreateFMul(val, ConstantFP::get(args[0]->getType(), 127.0));
@@ -7967,28 +7968,33 @@ Value *SPIRVToLLVM::transGLSLExtInst(SPIRVExtInst *extInst, BasicBlock *bb) {
79677968

79687969
case GLSLstd450PackUnorm4x8: {
79697970
// Convert <4 x float> into unsigned normalized <4 x i8> then pack into i32.
7971+
// round(clamp(c, 0, +1) * 255.0)
79707972
Value *val = getBuilder()->CreateFClamp(args[0], Constant::getNullValue(args[0]->getType()),
79717973
ConstantFP::get(args[0]->getType(), 1.0));
79727974
val = getBuilder()->CreateFMul(val, ConstantFP::get(args[0]->getType(), 255.0));
7975+
val = getBuilder()->CreateUnaryIntrinsic(Intrinsic::rint, val);
79737976
val = getBuilder()->CreateFPToUI(val, FixedVectorType::get(getBuilder()->getInt8Ty(), 4));
79747977
return getBuilder()->CreateBitCast(val, getBuilder()->getInt32Ty());
79757978
}
79767979

79777980
case GLSLstd450PackSnorm2x16: {
79787981
// Convert <2 x float> into signed normalized <2 x i16> then pack into i32.
7982+
// round(clamp(c, -1, +1) * 32767.0)
79797983
Value *val = getBuilder()->CreateFClamp(args[0], ConstantFP::get(args[0]->getType(), -1.0),
79807984
ConstantFP::get(args[0]->getType(), 1.0));
79817985
val = getBuilder()->CreateFMul(val, ConstantFP::get(args[0]->getType(), 32767.0));
7986+
val = getBuilder()->CreateUnaryIntrinsic(Intrinsic::rint, val);
79827987
val = getBuilder()->CreateFPToSI(val, FixedVectorType::get(getBuilder()->getInt16Ty(), 2));
79837988
return getBuilder()->CreateBitCast(val, getBuilder()->getInt32Ty());
79847989
}
79857990

79867991
case GLSLstd450PackUnorm2x16: {
7987-
// Convert <2 x float> into unsigned normalized <2 x i16> then pack into
7988-
// i32.
7992+
// Convert <2 x float> into unsigned normalized <2 x i16> then pack into i32.
7993+
// round(clamp(c, 0, +1) * 65535.0)
79897994
Value *val = getBuilder()->CreateFClamp(args[0], Constant::getNullValue(args[0]->getType()),
79907995
ConstantFP::get(args[0]->getType(), 1.0));
79917996
val = getBuilder()->CreateFMul(val, ConstantFP::get(args[0]->getType(), 65535.0));
7997+
val = getBuilder()->CreateUnaryIntrinsic(Intrinsic::rint, val);
79927998
val = getBuilder()->CreateFPToUI(val, FixedVectorType::get(getBuilder()->getInt16Ty(), 2));
79937999
return getBuilder()->CreateBitCast(val, getBuilder()->getInt32Ty());
79948000
}

0 commit comments

Comments
 (0)