Skip to content

Commit

Permalink
kram - update hlslparser
Browse files Browse the repository at this point in the history
Add Depth2D support.  Metal only allows sample/gather_compare from depth2d textures and their variants.  So get this started with 2d case first.  Tried setting address spaces up, but Metal seems to only want them declared as "thread" space (except ubo).

Note that MSL has array indexing into buffers which mimics StructuredBuffer.  That will need correct designators on the pointer to the array.   This may also need to apply to ConstantBuffer which can have an array size that is not currently parsed.

Remove more dead code.
  • Loading branch information
alecazam committed Mar 13, 2023
1 parent 370e82f commit bcdad2f
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 188 deletions.
32 changes: 15 additions & 17 deletions hlslparser/outshaders/ShaderHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ typedef uint16_t4 ushort4;
//typedef float64_t3 double3;
//typedef float64_t4 double4;

// 6.6 is cutting edge, want to target 6.2 for now
#define SM66 1
#if SM66
// compile to SM6.6 for these
typedef uint8_t4_packed uchar4_packed;
typedef int8_t4_packed char4_packed;
Expand Down Expand Up @@ -113,6 +116,7 @@ char4_packed fromInt4(int4 v, bool clamp = true)
{
return clamp ? pack_clamp_s8(v) : pack_s8(v);
}
#endif


#define USE_HALF 1
Expand Down Expand Up @@ -201,33 +205,27 @@ float4 SampleGrad(Texture2D<float4> t, SamplerState s, float2 texCoord, float2 g
{
return t.SampleGrad(s, texCoord.xy, gradx, grady);
}

//----------

// typedef Texture2D Depth2D;

// can just use the
//float4 Sample(Texture2D<float4> t, SamplerState s, float2 texCoord, int2 offset = 0)
//{
// return t.Sample(s, texCoord.xy, offset);
//}

// For persp shadows, remember to divide z = z/w before calling, or w = z/w on cube
float4 SampleCmp(Texture2D t, SamplerComparisonState s, float4 texCoord, int2 offset = 0)
float4 SampleCmp(Texture2D<float4> t, SamplerComparisonState s, float4 texCoord, int2 offset = 0)
{
return t.SampleCmp(s, texCoord.xy, texCoord.z, offset);
}


// no offset
float4 SampleCmp(TextureCube t, SamplerComparisonState s, float4 texCoord)
float4 GatherCmp(Texture2D<float4> t, SamplerComparisonState s, float4 texCoord, int2 offset = 0)
{
return t.SampleCmp(s, texCoord.xyz, texCoord.w);
return t.GatherCmp(s, texCoord.xy, texCoord.z, offset);
}

// TODO: may need to add to HLSLParser intrinsics
//float4 SampleCmp(Texture2DArray t, SamplerComparisonState s, float4 texCoord, int2 offset = 0)
//{
// return t.SampleCmp(s, texCoord.xyz, offset);
//}
//
//float4 SampleCmp(TextureCubeArray t, SamplerComparisonState s, float4 texCoord)
//{
// return t.SampleCmp(s, texCoord.xyzw);
//}

//----------

// this doesn't use SamplerState, raw load
Expand Down
17 changes: 17 additions & 0 deletions hlslparser/outshaders/ShaderMSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@ float4 Sample(texturecube<float> t, sampler s, float3 texCoord) {
float4 Sample(texturecube_array<float> t, sampler s, float4 texCoord) {
return t.sample(s, texCoord.xyz, uint(texCoord.w));
}
//----------

float4 Sample(depth2d<float> t, sampler s, float2 texCoord, int2 offset = 0)
{
return t.sample(s, texCoord.xy, offset);
}

// For persp shadows, remember to divide z = z/w before calling, or w = z/w on cube
float4 SampleCmp(depth2d<float> t, sampler s, float4 texCoord, int2 offset = 0)
{
return t.sample_compare(s, texCoord.xy, texCoord.z, offset);
}

float4 GatherCmp(depth2d<float> t, sampler s, float4 texCoord, int2 offset = 0)
{
return t.gather_compare(s, texCoord.xy, texCoord.z, offset);
}


// ----
Expand Down
2 changes: 1 addition & 1 deletion hlslparser/shaders/Sample.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// from here https://github.com/microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12Multithreading/src/shaders.hlsl

Texture2D<float4> shadowMap : register(t0);
Depth2D<float4> shadowMap : register(t0);
Texture2D<float4> diffuseMap : register(t1);
Texture2D<float4> normalMap : register(t2);

Expand Down
78 changes: 9 additions & 69 deletions hlslparser/src/HLSLGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const char* HLSLGenerator::GetTypeName(const HLSLType& type)
{
// samplers
case HLSLBaseType_SamplerState: return "SamplerState";

// can only pair this with depth texture to match Metal
case HLSLBaseType_SamplerComparisonState: return "SamplerComparisonState";
default: break;
}
Expand All @@ -46,6 +48,10 @@ const char* HLSLGenerator::GetTypeName(const HLSLType& type)
{
switch (baseType)
{
// depth textures just use Texture2D typedef
// TODO: add cube, array, ms, others
case HLSLBaseType_Depth2D: return "Depth2D";

case HLSLBaseType_Texture2D: return "Texture2D";
case HLSLBaseType_Texture2DArray: return "Texture2DArray";
case HLSLBaseType_Texture3D: return "Texture3D";
Expand Down Expand Up @@ -322,49 +328,7 @@ void HLSLGenerator::OutputExpression(HLSLExpression* expression)
HLSLIdentifierExpression* identifierExpression = static_cast<HLSLIdentifierExpression*>(expression);
const char* name = identifierExpression->name;

/* I don't want to pass structs
if (IsSamplerType(identifierExpression->expressionType) && identifierExpression->global)
{
// @@ Handle generic sampler type.
// Stupid HLSL doesn't have ctors, so have ctor calls.
if (identifierExpression->expressionType.baseType == HLSLBaseType_Texture2D)
{
if (identifierExpression->expressionType.textureType == HLSLBaseType_Half) // TODO: && !m_options.treatHalfAsFloat)
{
m_writer.Write("Texture2DHalfSamplerCtor(%s_texture, %s_sampler)", name, name);
}
else
{
m_writer.Write("Texture2DSamplerCtor(%s_texture, %s_sampler)", name, name);
}
// Remove this, so have half support above
//m_writer.Write("%s(%s_texture, %s_sampler)", "Texture2DSamplerCtor", name, name);
}
else if (identifierExpression->expressionType.baseType == HLSLBaseType_Sampler3D)
{
m_writer.Write("%s(%s_texture, %s_sampler)", "Texture3DSamplerCtor", name, name);
}
else if (identifierExpression->expressionType.baseType == HLSLBaseType_SamplerCube)
{
m_writer.Write("%s(%s_texture, %s_sampler)", "TextureCubeSamplerCtor", name, name);
}
else if (identifierExpression->expressionType.baseType == HLSLBaseType_Sampler2DShadow)
{
m_writer.Write("%s(%s_texture, %s_sampler)", "Texture2DShadowSamplerCtor", name, name);
}
// TODO: add all ctor types
else if (identifierExpression->expressionType.baseType == HLSLBaseType_Sampler2DMS)
{
m_writer.Write("%s", name);
}
}
else
*/
{
m_writer.Write("%s", name);
}
m_writer.Write("%s", name);
}
else if (expression->nodeType == HLSLNodeType_CastingExpression)
{
Expand Down Expand Up @@ -893,32 +857,8 @@ void HLSLGenerator::OutputDeclaration(HLSLDeclaration* declaration)
// caller to specify more types.

// texture carts the dimension and format
const char* textureType = NULL;
if (declaration->type.baseType == HLSLBaseType_Texture2D)
{
textureType = "Texture2D";
}
else if (declaration->type.baseType == HLSLBaseType_Texture2DArray)
{
textureType = "Texture2DArray";
}
else if (declaration->type.baseType == HLSLBaseType_Texture3D)
{
textureType = "Texture3D";
}
else if (declaration->type.baseType == HLSLBaseType_TextureCube)
{
textureType = "TextureCube";
}
else if (declaration->type.baseType == HLSLBaseType_TextureCubeArray)
{
textureType = "TextureCubeArray";
}
else if (declaration->type.baseType == HLSLBaseType_Texture2DMS)
{
textureType = "Texture2DMS";
}

const char* textureType = GetTypeName(declaration->type);

if (textureType != NULL)
{
if (reg != -1)
Expand Down
17 changes: 17 additions & 0 deletions hlslparser/src/HLSLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ bool IsTextureType(HLSLBaseType baseType)
return baseTypeDescriptions[baseType].coreType == CoreType_Texture;
}

bool IsDepthTextureType(HLSLBaseType baseType)
{
// return baseTypeDescriptions[baseType].coreType == CoreType_DepthTexture;
return baseType == HLSLBaseType_Depth2D;
}


bool IsBufferType(HLSLBaseType baseType)
{
return baseTypeDescriptions[baseType].coreType == CoreType_Buffer;
Expand Down Expand Up @@ -843,6 +850,11 @@ const Intrinsic _intrinsic[] =
TEXTURE_INTRINSIC_FUNCTION("Sample", HLSLBaseType_TextureCube, HLSLBaseType_Float3),
TEXTURE_INTRINSIC_FUNCTION("Sample", HLSLBaseType_TextureCubeArray, HLSLBaseType_Float4),

// Depth
TEXTURE_INTRINSIC_FUNCTION("Sample", HLSLBaseType_Depth2D, HLSLBaseType_Float2),
// TODO: TEXTURE_INTRINSIC_FUNCTION("SampleCmp", HLSLBaseType_Depth2D, HLSLBaseType_Float4),
// TODO: TEXTURE_INTRINSIC_FUNCTION("GatherCmp", HLSLBaseType_Depth2D, HLSLBaseType_Float4),

// one more dimension than Sample
TEXTURE_INTRINSIC_FUNCTION("SampleLevel", HLSLBaseType_Texture2D, HLSLBaseType_Float3),
TEXTURE_INTRINSIC_FUNCTION("SampleLevel", HLSLBaseType_Texture3D, HLSLBaseType_Float4),
Expand Down Expand Up @@ -959,6 +971,7 @@ const BaseTypeDescription baseTypeDescriptions[HLSLBaseType_Count] =
{ "TextureCubeArray", CoreType_Texture, DimensionType_None, NumericType_NaN, 1, 0, 0, -1 }, // HLSLBaseType_TextureCubeArray
{ "Texture2DMS", CoreType_Texture, DimensionType_None, NumericType_NaN, 1, 0, 0, -1 }, // HLSLBaseType_Texture2DMS

{ "Depth2D", CoreType_Texture, DimensionType_None, NumericType_NaN, 1, 0, 0, -1 }, // HLSLBaseType_Depth2D

{ "SamplerState", CoreType_Sampler, DimensionType_None, NumericType_NaN, 1, 0, 0, -1 }, // HLSLBaseType_Sampler
{ "SamplerComparisonState", CoreType_Sampler, DimensionType_None, NumericType_NaN, 1, 0, 0, -1 }, // HLSLBaseType_SamplerComparisonState
Expand Down Expand Up @@ -3736,6 +3749,10 @@ bool HLSLParser::AcceptType(bool allowVoid, HLSLType& type/*, bool acceptFlags*/
case HLSLToken_TextureCubeArray:
type.baseType = HLSLBaseType_TextureCubeArray;
break;

case HLSLToken_Depth2D:
type.baseType = HLSLBaseType_Depth2D;
break;

case HLSLToken_SamplerState:
type.baseType = HLSLBaseType_SamplerState;
Expand Down
1 change: 1 addition & 0 deletions hlslparser/src/HLSLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ extern bool IsMatrixType(HLSLBaseType baseType);
extern bool IsVectorType(HLSLBaseType baseType);
extern bool IsScalarType(HLSLBaseType baseType);
extern bool IsTextureType(HLSLBaseType baseType);
extern bool IsDepthTextureType(HLSLBaseType baseType);
extern bool IsBufferType(HLSLBaseType baseType);
extern bool IsNumericType(HLSLBaseType baseType);

Expand Down
20 changes: 5 additions & 15 deletions hlslparser/src/HLSLTokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,11 @@ static const char* _reservedWords[] =
"TextureCubeArray",
"Texture2DMS",

"Depth2D",

"SamplerState",
"SamplerComparisonState",
/*
// TODO: eliminate DX9 types
"texture",
"sampler",
"sampler2D",
"sampler3D",
"samplerCUBE",
"sampler2DShadow",
"sampler2DMS",
"sampler2DArray",
*/


"if",
"else",
"for",
Expand All @@ -94,11 +84,11 @@ static const char* _reservedWords[] =
"void",
"struct",

// DX9
// DX9 buffer types (tons of globals)
"cbuffer",
"tbuffer",

// DX10 templated types
// DX10 buffer templated types
"ConstantBuffer", // indexable cbuffer
"StructuredBuffer",
"RWStructuredBuffer",
Expand Down
3 changes: 3 additions & 0 deletions hlslparser/src/HLSLTokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ enum HLSLToken
HLSLToken_TextureCubeArray,
HLSLToken_Texture2DMS,

HLSLToken_Depth2D,
// TODO: other depth types

HLSLToken_SamplerState,
HLSLToken_SamplerComparisonState,

Expand Down
3 changes: 3 additions & 0 deletions hlslparser/src/HLSLTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ enum HLSLBaseType
HLSLBaseType_TextureCubeArray,
HLSLBaseType_Texture2DMS,

HLSLBaseType_Depth2D,
// TODO: add more depth types as needed (pair with SamplerComparisonState)

// Only 2 sampler types.
HLSLBaseType_SamplerState,
HLSLBaseType_SamplerComparisonState,
Expand Down
Loading

0 comments on commit bcdad2f

Please sign in to comment.