Merge pull request #347 from KhronosGroup/fix-340

Add packing support to HLSL
This commit is contained in:
Hans-Kristian Arntzen 2017-11-27 15:37:50 +01:00 committed by GitHub
commit 7b73d135f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 529 additions and 0 deletions

View File

@ -0,0 +1,44 @@
static float2 FP32Out;
static uint FP16;
static uint FP16Out;
static float2 FP32;
struct SPIRV_Cross_Input
{
nointerpolation uint FP16 : TEXCOORD0;
nointerpolation float2 FP32 : TEXCOORD1;
};
struct SPIRV_Cross_Output
{
float2 FP32Out : SV_Target0;
uint FP16Out : SV_Target1;
};
uint SPIRV_Cross_packHalf2x16(float2 value)
{
uint2 Packed = f32tof16(value);
return Packed.x | (Packed.y << 16);
}
float2 SPIRV_Cross_unpackHalf2x16(uint value)
{
return f16tof32(uint2(value & 0xffff, value >> 16));
}
void frag_main()
{
FP32Out = SPIRV_Cross_unpackHalf2x16(FP16);
FP16Out = SPIRV_Cross_packHalf2x16(FP32);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
FP16 = stage_input.FP16;
FP32 = stage_input.FP32;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FP32Out = FP32Out;
stage_output.FP16Out = FP16Out;
return stage_output;
}

View File

@ -0,0 +1,109 @@
static float4 FP32Out;
static uint UNORM8;
static uint SNORM8;
static uint UNORM16;
static uint SNORM16;
static uint UNORM8Out;
static float4 FP32;
static uint SNORM8Out;
static uint UNORM16Out;
static uint SNORM16Out;
struct SPIRV_Cross_Input
{
nointerpolation uint SNORM8 : TEXCOORD0;
nointerpolation uint UNORM8 : TEXCOORD1;
nointerpolation uint SNORM16 : TEXCOORD2;
nointerpolation uint UNORM16 : TEXCOORD3;
nointerpolation float4 FP32 : TEXCOORD4;
};
struct SPIRV_Cross_Output
{
float4 FP32Out : SV_Target0;
uint UNORM8Out : SV_Target1;
uint SNORM8Out : SV_Target2;
uint UNORM16Out : SV_Target3;
uint SNORM16Out : SV_Target4;
};
uint SPIRV_Cross_packUnorm4x8(float4 value)
{
uint4 Packed = uint4(round(saturate(value) * 255.0));
return Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24);
}
float4 SPIRV_Cross_unpackUnorm4x8(uint value)
{
uint4 Packed = uint4(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, value >> 24);
return float4(Packed) / 255.0;
}
uint SPIRV_Cross_packSnorm4x8(float4 value)
{
int4 Packed = int4(round(clamp(value, -1.0, 1.0) * 127.0)) & 0xff;
return uint(Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24));
}
float4 SPIRV_Cross_unpackSnorm4x8(uint value)
{
int SignedValue = int(value);
int4 Packed = int4(SignedValue << 24, SignedValue << 16, SignedValue << 8, SignedValue) >> 24;
return clamp(float4(Packed) / 127.0, -1.0, 1.0);
}
uint SPIRV_Cross_packUnorm2x16(float2 value)
{
uint2 Packed = uint2(round(saturate(value) * 65535.0));
return Packed.x | (Packed.y << 16);
}
float2 SPIRV_Cross_unpackUnorm2x16(uint value)
{
uint2 Packed = uint2(value & 0xffff, value >> 16);
return float2(Packed) / 65535.0;
}
uint SPIRV_Cross_packSnorm2x16(float2 value)
{
int2 Packed = int2(round(clamp(value, -1.0, 1.0) * 32767.0)) & 0xffff;
return uint(Packed.x | (Packed.y << 16));
}
float2 SPIRV_Cross_unpackSnorm2x16(uint value)
{
int SignedValue = int(value);
int2 Packed = int2(SignedValue << 16, SignedValue) >> 16;
return clamp(float2(Packed) / 32767.0, -1.0, 1.0);
}
void frag_main()
{
FP32Out = SPIRV_Cross_unpackUnorm4x8(UNORM8);
FP32Out = SPIRV_Cross_unpackSnorm4x8(SNORM8);
float2 _21 = SPIRV_Cross_unpackUnorm2x16(UNORM16);
FP32Out = float4(_21.x, _21.y, FP32Out.z, FP32Out.w);
float2 _26 = SPIRV_Cross_unpackSnorm2x16(SNORM16);
FP32Out = float4(_26.x, _26.y, FP32Out.z, FP32Out.w);
UNORM8Out = SPIRV_Cross_packUnorm4x8(FP32);
SNORM8Out = SPIRV_Cross_packSnorm4x8(FP32);
UNORM16Out = SPIRV_Cross_packUnorm2x16(FP32.xy);
SNORM16Out = SPIRV_Cross_packSnorm2x16(FP32.zw);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
UNORM8 = stage_input.UNORM8;
SNORM8 = stage_input.SNORM8;
UNORM16 = stage_input.UNORM16;
SNORM16 = stage_input.SNORM16;
FP32 = stage_input.FP32;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FP32Out = FP32Out;
stage_output.UNORM8Out = UNORM8Out;
stage_output.SNORM8Out = SNORM8Out;
stage_output.UNORM16Out = UNORM16Out;
stage_output.SNORM16Out = SNORM16Out;
return stage_output;
}

View File

@ -0,0 +1,44 @@
static float2 FP32Out;
static uint FP16;
static uint FP16Out;
static float2 FP32;
struct SPIRV_Cross_Input
{
nointerpolation uint FP16 : TEXCOORD0;
nointerpolation float2 FP32 : TEXCOORD1;
};
struct SPIRV_Cross_Output
{
float2 FP32Out : SV_Target0;
uint FP16Out : SV_Target1;
};
uint SPIRV_Cross_packHalf2x16(float2 value)
{
uint2 Packed = f32tof16(value);
return Packed.x | (Packed.y << 16);
}
float2 SPIRV_Cross_unpackHalf2x16(uint value)
{
return f16tof32(uint2(value & 0xffff, value >> 16));
}
void frag_main()
{
FP32Out = SPIRV_Cross_unpackHalf2x16(FP16);
FP16Out = SPIRV_Cross_packHalf2x16(FP32);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
FP16 = stage_input.FP16;
FP32 = stage_input.FP32;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FP32Out = FP32Out;
stage_output.FP16Out = FP16Out;
return stage_output;
}

View File

@ -0,0 +1,109 @@
static float4 FP32Out;
static uint UNORM8;
static uint SNORM8;
static uint UNORM16;
static uint SNORM16;
static uint UNORM8Out;
static float4 FP32;
static uint SNORM8Out;
static uint UNORM16Out;
static uint SNORM16Out;
struct SPIRV_Cross_Input
{
nointerpolation uint SNORM8 : TEXCOORD0;
nointerpolation uint UNORM8 : TEXCOORD1;
nointerpolation uint SNORM16 : TEXCOORD2;
nointerpolation uint UNORM16 : TEXCOORD3;
nointerpolation float4 FP32 : TEXCOORD4;
};
struct SPIRV_Cross_Output
{
float4 FP32Out : SV_Target0;
uint UNORM8Out : SV_Target1;
uint SNORM8Out : SV_Target2;
uint UNORM16Out : SV_Target3;
uint SNORM16Out : SV_Target4;
};
uint SPIRV_Cross_packUnorm4x8(float4 value)
{
uint4 Packed = uint4(round(saturate(value) * 255.0));
return Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24);
}
float4 SPIRV_Cross_unpackUnorm4x8(uint value)
{
uint4 Packed = uint4(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, value >> 24);
return float4(Packed) / 255.0;
}
uint SPIRV_Cross_packSnorm4x8(float4 value)
{
int4 Packed = int4(round(clamp(value, -1.0, 1.0) * 127.0)) & 0xff;
return uint(Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24));
}
float4 SPIRV_Cross_unpackSnorm4x8(uint value)
{
int SignedValue = int(value);
int4 Packed = int4(SignedValue << 24, SignedValue << 16, SignedValue << 8, SignedValue) >> 24;
return clamp(float4(Packed) / 127.0, -1.0, 1.0);
}
uint SPIRV_Cross_packUnorm2x16(float2 value)
{
uint2 Packed = uint2(round(saturate(value) * 65535.0));
return Packed.x | (Packed.y << 16);
}
float2 SPIRV_Cross_unpackUnorm2x16(uint value)
{
uint2 Packed = uint2(value & 0xffff, value >> 16);
return float2(Packed) / 65535.0;
}
uint SPIRV_Cross_packSnorm2x16(float2 value)
{
int2 Packed = int2(round(clamp(value, -1.0, 1.0) * 32767.0)) & 0xffff;
return uint(Packed.x | (Packed.y << 16));
}
float2 SPIRV_Cross_unpackSnorm2x16(uint value)
{
int SignedValue = int(value);
int2 Packed = int2(SignedValue << 16, SignedValue) >> 16;
return clamp(float2(Packed) / 32767.0, -1.0, 1.0);
}
void frag_main()
{
FP32Out = SPIRV_Cross_unpackUnorm4x8(UNORM8);
FP32Out = SPIRV_Cross_unpackSnorm4x8(SNORM8);
float2 _21 = SPIRV_Cross_unpackUnorm2x16(UNORM16);
FP32Out = float4(_21.x, _21.y, FP32Out.z, FP32Out.w);
float2 _26 = SPIRV_Cross_unpackSnorm2x16(SNORM16);
FP32Out = float4(_26.x, _26.y, FP32Out.z, FP32Out.w);
UNORM8Out = SPIRV_Cross_packUnorm4x8(FP32);
SNORM8Out = SPIRV_Cross_packSnorm4x8(FP32);
UNORM16Out = SPIRV_Cross_packUnorm2x16(FP32.xy);
SNORM16Out = SPIRV_Cross_packSnorm2x16(FP32.zw);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
UNORM8 = stage_input.UNORM8;
SNORM8 = stage_input.SNORM8;
UNORM16 = stage_input.UNORM16;
SNORM16 = stage_input.SNORM16;
FP32 = stage_input.FP32;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FP32Out = FP32Out;
stage_output.UNORM8Out = UNORM8Out;
stage_output.SNORM8Out = SNORM8Out;
stage_output.UNORM16Out = UNORM16Out;
stage_output.SNORM16Out = SNORM16Out;
return stage_output;
}

View File

@ -0,0 +1,12 @@
#version 450
layout(location = 0) flat in uint FP16;
layout(location = 1) flat in vec2 FP32;
layout(location = 0) out vec2 FP32Out;
layout(location = 1) out uint FP16Out;
void main()
{
FP32Out = unpackHalf2x16(FP16);
FP16Out = packHalf2x16(FP32);
}

View File

@ -0,0 +1,24 @@
#version 450
layout(location = 0) flat in uint SNORM8;
layout(location = 1) flat in uint UNORM8;
layout(location = 2) flat in uint SNORM16;
layout(location = 3) flat in uint UNORM16;
layout(location = 4) flat in vec4 FP32;
layout(location = 0) out vec4 FP32Out;
layout(location = 1) out uint UNORM8Out;
layout(location = 2) out uint SNORM8Out;
layout(location = 3) out uint UNORM16Out;
layout(location = 4) out uint SNORM16Out;
void main()
{
FP32Out = unpackUnorm4x8(UNORM8);
FP32Out = unpackSnorm4x8(SNORM8);
FP32Out.xy = unpackUnorm2x16(UNORM16);
FP32Out.xy = unpackSnorm2x16(SNORM16);
UNORM8Out = packUnorm4x8(FP32);
SNORM8Out = packSnorm4x8(FP32);
UNORM16Out = packUnorm2x16(FP32.xy);
SNORM16Out = packSnorm2x16(FP32.zw);
}

View File

@ -1183,6 +1183,94 @@ void CompilerHLSL::emit_resources()
}
}
}
if (requires_fp16_packing)
{
// HLSL does not pack into a single word sadly :(
statement("uint SPIRV_Cross_packHalf2x16(float2 value)");
begin_scope();
statement("uint2 Packed = f32tof16(value);");
statement("return Packed.x | (Packed.y << 16);");
end_scope();
statement("");
statement("float2 SPIRV_Cross_unpackHalf2x16(uint value)");
begin_scope();
statement("return f16tof32(uint2(value & 0xffff, value >> 16));");
end_scope();
statement("");
}
// HLSL does not seem to have builtins for these operation, so roll them by hand ...
if (requires_unorm8_packing)
{
statement("uint SPIRV_Cross_packUnorm4x8(float4 value)");
begin_scope();
statement("uint4 Packed = uint4(round(saturate(value) * 255.0));");
statement("return Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24);");
end_scope();
statement("");
statement("float4 SPIRV_Cross_unpackUnorm4x8(uint value)");
begin_scope();
statement("uint4 Packed = uint4(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, value >> 24);");
statement("return float4(Packed) / 255.0;");
end_scope();
statement("");
}
if (requires_snorm8_packing)
{
statement("uint SPIRV_Cross_packSnorm4x8(float4 value)");
begin_scope();
statement("int4 Packed = int4(round(clamp(value, -1.0, 1.0) * 127.0)) & 0xff;");
statement("return uint(Packed.x | (Packed.y << 8) | (Packed.z << 16) | (Packed.w << 24));");
end_scope();
statement("");
statement("float4 SPIRV_Cross_unpackSnorm4x8(uint value)");
begin_scope();
statement("int SignedValue = int(value);");
statement("int4 Packed = int4(SignedValue << 24, SignedValue << 16, SignedValue << 8, SignedValue) >> 24;");
statement("return clamp(float4(Packed) / 127.0, -1.0, 1.0);");
end_scope();
statement("");
}
if (requires_unorm16_packing)
{
statement("uint SPIRV_Cross_packUnorm2x16(float2 value)");
begin_scope();
statement("uint2 Packed = uint2(round(saturate(value) * 65535.0));");
statement("return Packed.x | (Packed.y << 16);");
end_scope();
statement("");
statement("float2 SPIRV_Cross_unpackUnorm2x16(uint value)");
begin_scope();
statement("uint2 Packed = uint2(value & 0xffff, value >> 16);");
statement("return float2(Packed) / 65535.0;");
end_scope();
statement("");
}
if (requires_snorm16_packing)
{
statement("uint SPIRV_Cross_packSnorm2x16(float2 value)");
begin_scope();
statement("int2 Packed = int2(round(clamp(value, -1.0, 1.0) * 32767.0)) & 0xffff;");
statement("return uint(Packed.x | (Packed.y << 16));");
end_scope();
statement("");
statement("float2 SPIRV_Cross_unpackSnorm2x16(uint value)");
begin_scope();
statement("int SignedValue = int(value);");
statement("int2 Packed = int2(SignedValue << 16, SignedValue) >> 16;");
statement("return clamp(float2(Packed) / 32767.0, -1.0, 1.0);");
end_scope();
statement("");
}
}
string CompilerHLSL::layout_for_member(const SPIRType &type, uint32_t index)
@ -2209,6 +2297,100 @@ void CompilerHLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
emit_binary_func_op(result_type, id, args[0], args[1], "EvaluateAttributeSnapped");
break;
case GLSLstd450PackHalf2x16:
if (!requires_fp16_packing)
{
requires_fp16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packHalf2x16");
break;
case GLSLstd450UnpackHalf2x16:
if (!requires_fp16_packing)
{
requires_fp16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackHalf2x16");
break;
case GLSLstd450PackSnorm4x8:
if (!requires_snorm8_packing)
{
requires_snorm8_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packSnorm4x8");
break;
case GLSLstd450UnpackSnorm4x8:
if (!requires_snorm8_packing)
{
requires_snorm8_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackSnorm4x8");
break;
case GLSLstd450PackUnorm4x8:
if (!requires_unorm8_packing)
{
requires_unorm8_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packUnorm4x8");
break;
case GLSLstd450UnpackUnorm4x8:
if (!requires_unorm8_packing)
{
requires_unorm8_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackUnorm4x8");
break;
case GLSLstd450PackSnorm2x16:
if (!requires_snorm16_packing)
{
requires_snorm16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packSnorm2x16");
break;
case GLSLstd450UnpackSnorm2x16:
if (!requires_snorm16_packing)
{
requires_snorm16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackSnorm2x16");
break;
case GLSLstd450PackUnorm2x16:
if (!requires_unorm16_packing)
{
requires_unorm16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_packUnorm2x16");
break;
case GLSLstd450UnpackUnorm2x16:
if (!requires_unorm16_packing)
{
requires_unorm16_packing = true;
force_recompile = true;
}
emit_unary_func_op(result_type, id, args[0], "SPIRV_Cross_unpackUnorm2x16");
break;
case GLSLstd450PackDouble2x32:
case GLSLstd450UnpackDouble2x32:
SPIRV_CROSS_THROW("packDouble2x32/unpackDouble2x32 not supported in HLSL.");
default:
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count);
break;

View File

@ -116,6 +116,11 @@ private:
Options options;
bool requires_op_fmod = false;
bool requires_textureProj = false;
bool requires_fp16_packing = false;
bool requires_unorm8_packing = false;
bool requires_snorm8_packing = false;
bool requires_unorm16_packing = false;
bool requires_snorm16_packing = false;
uint64_t required_textureSizeVariants = 0;
void require_texture_query_variant(const SPIRType &type);