glslang/Test/hlsl.texture.struct.frag
LoopDawg 5ee05891cf HLSL: add methods to track user structure in texture return type.
Some languages allow a restricted set of user structure types returned from texture sampling
operations.  Restrictions include the total vector size of all components may not exceed 4,
and the basic types of all members must be identical.

This adds underpinnings for that ability.  Because storing a whole TType or even a simple
TTypeList in the TSampler would be expensive, the structure definition is held in a
table outside the TType.  The TSampler contains a small bitfield index, currently 4 bits
to support up to 15 separate texture template structure types, but that can be adjusted
up or down.  Vector returns are handled as before.

There are abstraction methods accepting and returning a TType (such as may have been parsed
from a grammar).  The new methods will accept a texture template type and set the
sampler to the structure if possible, checking a range of error conditions such as whether
the total structure vector components exceed 4, or whether their basic types differe, or
whether the struct contains non-vector-or-scalar members.  Another query returns the
appropriate TType for the sampler.

High level summary of design:

In the TSampler, this holds an index into the texture structure return type table:

    unsigned int structReturnIndex : structReturnIndexBits;

These are the methods to set or get the return type from the TSampler.  They work for vector or structure returns, and potentially could be expanded to handle other things (small arrays?) if ever needed.

    bool setTextureReturnType(TSampler& sampler, const TType& retType, const TSourceLoc& loc);
    void getTextureReturnType(const TSampler& sampler, const TType& retType, const TSourceLoc& loc) const;

The ``convertReturn`` lambda in ``HlslParseContext::decomposeSampleMethods`` is greatly expanded to know how to copy a vec4 sample return to whatever the structure type should be.  This is a little awkward since it involves introducing a comma expression to return the proper aggregate value after a set of memberwise copies.
2017-08-15 16:40:21 -06:00

56 lines
1.2 KiB
GLSL

struct s1_t {
float c0;
float2 c1;
float c2;
};
struct s2_t {
float c0;
float3 c1;
};
struct s3_t {
float2 c0;
float1 c1;
};
struct s4_t {
int c0;
int2 c1;
int c2;
};
struct s5_t {
uint c0;
uint c1;
};
SamplerState g_sSamp;
Texture2D <s1_t> g_tTex2s1;
Texture2D <s2_t> g_tTex2s2;
Texture2D <s3_t> g_tTex2s3;
Texture2D <s4_t> g_tTex2s4;
Texture2D <s5_t> g_tTex2s5;
Texture2D <s1_t> g_tTex2s1a; // same type as g_tTex2s1, to test fn signature matching.
// function overloading to test name mangling with textures templatized on structs
s1_t fn1(Texture2D <s1_t> t1) { return t1 . Sample(g_sSamp, float2(0.6, 0.61)); }
s2_t fn1(Texture2D <s2_t> t2) { return t2 . Sample(g_sSamp, float2(0.6, 0.61)); }
float4 main() : SV_Target0
{
s1_t s1 = g_tTex2s1 . Sample(g_sSamp, float2(0.1, 0.11));
s2_t s2 = g_tTex2s2 . Sample(g_sSamp, float2(0.2, 0.21));
s3_t s3 = g_tTex2s3 . Sample(g_sSamp, float2(0.3, 0.31));
s4_t s4 = g_tTex2s4 . Sample(g_sSamp, float2(0.4, 0.41));
s5_t s5 = g_tTex2s5 . Sample(g_sSamp, float2(0.5, 0.51));
s1_t r0 = fn1(g_tTex2s1);
s2_t r1 = fn1(g_tTex2s2);
s1_t r2 = fn1(g_tTex2s1a);
return 0;
}