qt5base-lts/tests/manual/rhi/displacement/material_domain.hlsl
Laszlo Agocs 870a3011ed rhi: Add a displacement / tessellation manual test
There is something odd when running on Metal: note how the uv
is vec3 instead of vec2, in order to make the vertex-tesc-tese
data to look like this:

struct main0_out
{
    float3 out_uv;
    float3 out_normal;
    float4 gl_Position;
};

if out_uv was float2 we'd get some strange rendering results,
perhaps due to something related to alignment. But have no means
to investigate this further.

Change-Id: I79d4edb2ddde3971c599c4326d98e99a49aa7122
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
2023-02-14 17:48:09 +01:00

55 lines
1.5 KiB
HLSL

struct Input
{
float edges[3] : SV_TessFactor;
float inside : SV_InsideTessFactor;
};
struct PatchInput
{
float3 position : POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
};
struct PixelInput
{
//float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float4 position : SV_POSITION;
};
cbuffer buf : register(b0)
{
row_major float4x4 mvp : packoffset(c0);
float displacementAmount : packoffset(c4);
float tessInner : packoffset(c4.y);
float tessOuter : packoffset(c4.z);
int useTex : packoffset(c4.w);
};
Texture2D<float4> tex : register(t1);
SamplerState texsampler : register(s1);
[domain("tri")]
PixelInput main(Input input, float3 uvwCoord : SV_DomainLocation, const OutputPatch<PatchInput, 3> patch)
{
PixelInput output;
float3 pos = uvwCoord.x * patch[0].position + uvwCoord.y * patch[1].position + uvwCoord.z * patch[2].position;
float2 uv = uvwCoord.x * patch[0].uv + uvwCoord.y * patch[1].uv;
float3 normal = normalize(uvwCoord.x * patch[0].normal + uvwCoord.y * patch[1].normal + uvwCoord.z * patch[2].normal);
float4 c = tex.SampleLevel(texsampler, uv, 0.0);
const float3 yCoeff_709 = float3(0.2126, 0.7152, 0.0722);
float df = dot(c.rgb, yCoeff_709);
if (useTex == 0)
df = 1.0;
float3 displacedPos = pos + normal * df * displacementAmount;
output.position = mul(float4(displacedPos, 1.0), mvp);
//output.uv = uv;
output.normal = normal;
return output;
}