Commit 307b6507 by LoopDawg

HLSL: handle multiple clip/cull semantic IDs

HLSL allows several variables to be declared. There are packing rules involved: e.g, a float3 and a float1 can be packed into a single array[4], while for a float3 and another float3, the second one will skip the third array entry to avoid straddling This is implements that ability. Because there can be multiple variables involved, and the final output array will often be a different type altogether (to fuse the values into a single destination), a new variable is synthesized, unlike the prior clip/cull support which used the declared variable. The new variable name is taken from one of the declared ones, so the old tests are unchanged. Several new tests are added to test various packing scenarios. Only two semantic IDs are supported: 0, and 1, per HLSL rules. This is encapsulated in static const int maxClipCullRegs = 2; and the algorithm (probably :) ) generalizes to larger values, although there are a few issues around how HLSL would pack (e.g, would 4 scalars be packed into a single HLSL float4 out reg? Probably, and this algorithm assumes so).
parent cd52fd5a
......@@ -130,6 +130,7 @@ Shader version: 500
0:? Linker Objects
0:? '@entryPointOutput' (layout( location=0) out structure{})
0:? 'Position' (layout( location=0) in 4-component vector of float)
0:? '@entryPointOutput_ClipRect' ( out 4-element array of float ClipDistance)
Linked vertex stage:
......@@ -266,6 +267,7 @@ Shader version: 500
0:? Linker Objects
0:? '@entryPointOutput' (layout( location=0) out structure{})
0:? 'Position' (layout( location=0) in 4-component vector of float)
0:? '@entryPointOutput_ClipRect' ( out 4-element array of float ClipDistance)
// Module Version 10000
// Generated by (magic number): 80001
......
......@@ -154,6 +154,7 @@ Shader version: 500
0:? Linker Objects
0:? '@entryPointOutput' (layout( location=0) out structure{})
0:? 'Position' (layout( location=0) in 4-component vector of float)
0:? '@entryPointOutput_ClipRect' ( out 4-element array of float ClipDistance)
Linked vertex stage:
......@@ -314,6 +315,7 @@ Shader version: 500
0:? Linker Objects
0:? '@entryPointOutput' (layout( location=0) out structure{})
0:? 'Position' (layout( location=0) in 4-component vector of float)
0:? '@entryPointOutput_ClipRect' ( out 4-element array of float ClipDistance)
// Module Version 10000
// Generated by (magic number): 80001
......
......@@ -69,6 +69,8 @@ output primitive = line_strip
0:? '@entryPointOutput' (layout( location=0) out structure{ temp int ii})
0:? 'VertexID' (layout( location=0) in 3-element array of uint)
0:? 'OutputStream' (layout( location=1) out structure{ temp int ii})
0:? 'OutputStream_clip0' ( out 1-element array of float ClipDistance)
0:? 'OutputStream_cull0' ( out 1-element array of float CullDistance)
0:? 'OutputStream_vpai' ( out uint ViewportIndex)
......@@ -145,6 +147,8 @@ output primitive = line_strip
0:? '@entryPointOutput' (layout( location=0) out structure{ temp int ii})
0:? 'VertexID' (layout( location=0) in 3-element array of uint)
0:? 'OutputStream' (layout( location=1) out structure{ temp int ii})
0:? 'OutputStream_clip0' ( out 1-element array of float ClipDistance)
0:? 'OutputStream_cull0' ( out 1-element array of float CullDistance)
0:? 'OutputStream_vpai' ( out uint ViewportIndex)
// Module Version 10000
......
struct VS_OUTPUT {
float4 Position : SV_Position;
float4 clip0 : SV_ClipDistance0; // multiple semantic IDs, two vec4s (no extra packing)
float4 clip1 : SV_ClipDistance1; // ...
};
VS_OUTPUT main()
{
VS_OUTPUT Output;
Output.Position = 0;
Output.clip0.x = 0;
Output.clip0.y = 1;
Output.clip0.z = 2;
Output.clip0.w = 3;
Output.clip1.x = 4;
Output.clip1.y = 5;
Output.clip1.z = 6;
Output.clip1.w = 7;
return Output;
}
struct VS_OUTPUT {
float4 Position : SV_Position;
float3 clip0 : SV_ClipDistance0; // multiple semantic IDs, vec3+vec4 (skip)
float4 clip1 : SV_ClipDistance1; // ...
};
VS_OUTPUT main()
{
VS_OUTPUT Output;
Output.Position = 0;
Output.clip0.x = 0;
Output.clip0.y = 1;
Output.clip0.z = 2;
// Position 3 is skipped
Output.clip1.x = 4;
Output.clip1.y = 5;
Output.clip1.z = 6;
Output.clip1.w = 7;
return Output;
}
struct VS_OUTPUT {
float4 Position : SV_Position;
float3 clip0 : SV_ClipDistance0; // multiple semantic IDs, vec3+float (pack)
float clip1 : SV_ClipDistance1; // ...
};
VS_OUTPUT main()
{
VS_OUTPUT Output;
Output.Position = 0;
Output.clip0.x = 0;
Output.clip0.y = 1;
Output.clip0.z = 2;
// Position 3 is packed from clip1's float
Output.clip1 = 3;
return Output;
}
struct VS_OUTPUT {
float4 Position : SV_Position;
};
// Test packing 0 and 1 semantics into single array[4] output, from out fn params.
VS_OUTPUT main(out float3 clip0 : SV_ClipDistance0, out float clip1 : SV_ClipDistance1)
{
VS_OUTPUT Output;
Output.Position = 0;
clip0.x = 0;
clip0.y = 1;
clip0.z = 2;
// Position 3 is packed from clip1's float
clip1 = 3;
return Output;
}
struct S {
float clip : SV_ClipDistance;
float clip0 : SV_ClipDistance0;
float clip7 : SV_ClipDistance7;
float cull : SV_CullDistance;
float cull2 : SV_CullDistance2;
float cull5 : SV_CullDistance5;
float clip1 : SV_ClipDistance1;
float cull0 : SV_CullDistance0;
float cull1 : SV_CullDistance1;
int ii : SV_InstanceID;
};
......
......@@ -102,6 +102,10 @@ INSTANTIATE_TEST_CASE_P(
{"hlsl.clipdistance-3.vert", "main"},
{"hlsl.clipdistance-4.vert", "main"},
{"hlsl.clipdistance-5.vert", "main"},
{"hlsl.clipdistance-6.vert", "main"},
{"hlsl.clipdistance-7.vert", "main"},
{"hlsl.clipdistance-8.vert", "main"},
{"hlsl.clipdistance-9.vert", "main"},
{"hlsl.comparison.vec.frag", "main"},
{"hlsl.conditional.frag", "PixelShaderFunction"},
{"hlsl.constantbuffer.frag", "main"},
......
......@@ -39,6 +39,8 @@
#include "../glslang/MachineIndependent/parseVersions.h"
#include "../glslang/MachineIndependent/ParseHelper.h"
#include <array>
namespace glslang {
class TAttributeMap; // forward declare
......@@ -91,7 +93,7 @@ public:
TIntermTyped* handleAssign(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
TIntermTyped* handleAssignToMatrixSwizzle(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
TIntermTyped* handleFunctionCall(const TSourceLoc&, TFunction*, TIntermTyped*);
TIntermAggregate* assignClipCullDistance(const TSourceLoc&, TOperator, TIntermTyped* left, TIntermTyped* right);
TIntermAggregate* assignClipCullDistance(const TSourceLoc&, TOperator, int semanticId, TIntermTyped* left, TIntermTyped* right);
void decomposeIntrinsic(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
void decomposeSampleMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
void decomposeStructBufferMethods(const TSourceLoc&, TIntermTyped*& node, TIntermNode* arguments);
......@@ -308,6 +310,10 @@ protected:
// Finalization step: remove unused buffer blocks from linkage (we don't know until the
// shader is entirely compiled)
void removeUnusedStructBufferCounters();
static bool isClipOrCullDistance(TBuiltInVariable);
static bool isClipOrCullDistance(const TQualifier& qual) { return isClipOrCullDistance(qual.builtIn); }
static bool isClipOrCullDistance(const TType& type) { return isClipOrCullDistance(type.getQualifier()); }
// Pass through to base class after remembering builtin mappings.
using TParseContextBase::trackLinkage;
......@@ -432,6 +438,13 @@ protected:
TVariable* gsStreamOutput; // geometry shader stream outputs, for emit (Append method)
TVariable* clipDistanceOutput; // synthesized clip distance output variable (shader might have >1)
TVariable* cullDistanceOutput; // synthesized cull distance output variable (shader might have >1)
static const int maxClipCullRegs = 2;
std::array<int, maxClipCullRegs> clipSemanticNSize; // vector, indexed by clip semantic ID
std::array<int, maxClipCullRegs> cullSemanticNSize; // vector, indexed by cull semantic ID
// This tracks the first (mip level) argument to the .mips[][] operator. Since this can be nested as
// in tx.mips[tx.mips[0][1].x][2], we need a stack. We also track the TSourceLoc for error reporting
// purposes.
......
......@@ -420,32 +420,6 @@ void HlslScanContext::fillInKeywordMap()
}
(*SemanticMap)["SV_POSITION"] = EbvPosition;
(*SemanticMap)["SV_CLIPDISTANCE"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE0"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE1"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE2"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE3"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE4"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE5"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE6"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE7"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE8"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE9"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE10"] = EbvClipDistance;
(*SemanticMap)["SV_CLIPDISTANCE11"] = EbvClipDistance;
(*SemanticMap)["SV_CULLDISTANCE"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE0"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE1"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE2"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE3"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE4"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE5"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE6"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE7"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE8"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE9"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE10"] = EbvCullDistance;
(*SemanticMap)["SV_CULLDISTANCE11"] = EbvCullDistance;
(*SemanticMap)["SV_VERTEXID"] = EbvVertexIndex;
(*SemanticMap)["SV_VIEWPORTARRAYINDEX"] = EbvViewportIndex;
(*SemanticMap)["SV_TESSFACTOR"] = EbvTessLevelOuter;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment