https://github.com/shader-slang/slang
Raw File
Tip revision: 3c92fedbb73f76f3418630069c880bdc8aedccb4 authored by Tim Foley on 10 July 2017, 17:58:47 UTC
Merge pull request #66 from tfoleyNV/falcor-work
Tip revision: 3c92fed
interface.slang
//TEST:SIMPLE:

// Confirm that basic `interface` syntax stuff type-checks.

// The example here is adapted from examples in Matt Pharr's
// chapter in GPU Gems: "An Introduction to Shader Interaces"

struct LightSample
{
	float3 C; // radiance
	float3 L; // direction	
};

interface Light
{
	LightSample illuminate(float3 P_world);
}

struct PointLight : Light
{
	float3 Plight_world;
	float3 C;

	LightSample illuminate(float3 P_world)
	{
		float3 delta = Plight_world - P_world;
		float3 L = normalize(delta);
		float distance = length(delta);

		LightSample result;
		result.L = L;
		result.C = C * (1 / (distance*distance));
		return result;
	}	
};

// using the concrete type directly
float3 A( float3 P_world, PointLight light )
{
	return light.illuminate(P_world).L;
}

// using the abstract interface type
float3 B( float3 P_world, Light light )
{
	return light.illuminate(P_world).L;
}

//
float3 Test(float3 P_world, PointLight pointLight, Light light)
{
	// dconcrete type expected, concrete type provided
	float3 a = A(P_world, pointLight);

	// abstract type expected, abstract type provided
	float3 b = B(P_world, light);

	// abstract type expected, concrete type provided
	float3 c = B(P_world, pointLight);

	// The remaining case (passing `Light` where `PointLight` is expected)
	// should be an error, so we want a distinct test for that.

	return a + b + c;
}
back to top