// This is a simple alpha-testing foliage-type shader used to render grass and tree leaves in the instancing
// example program.

// This is a constant buffer that will be set by the engine for each rendering pass.
// It is required that you provide this cbuffer with all your shaders.
cbuffer MatrixBuffer : register(b0) {	// The global MatrixBuffer cbuffer always uses register b0. Any custom cbuffers will use register b1..b15 (per shader) in order.
	float4x4 View;
	float4x4 Projection;
	float4x4 ViewProj;					// Precomputed on a per-frame basis
};

// Textures / samplers.
// The Texture2D corresponds to the texture(s) set to the limb / object rendered using this shader from within DBPro; 
// the sampler corresponds to the SamplerState(s) associated with the ShaderTechnique using this shader.
Texture2D	DiffuseMap;
sampler		DiffuseSampler;

// A special StructuredBuffer bound to register t24 is used to identify per-instance indexable transform matrices.
// This buffer is created, maintained and updated automatically by the Ziggurat engine and will take things such 
// as culling, exclusion and instance ordering into account.
struct sInstanceTransform {
	float4x4 World;
	float4x4 WorldViewProjection;
};

StructuredBuffer<sInstanceTransform> Transform : register(t24);

// Vertex input structure.
// It is vital that this one corresponds directly to the vertex layout of the object you are rendering using 
// this shader.
// Also note the SV_INSTANCEID semantic; this will contain the current instance ID being rendered and is 
// used to index into the Transform buffer declared above. This allows us to use separate world matrces for
// individual instances.
// The instance id field is NOT part of your vertex layout, but rather is a system value that is added to the 
// vertex data by the input assembler stage if you specify any vertex shader input using the SV_INSTANCEID 
// semantic.
struct vs_in {
	/* These correspond directly to the vertex layout the objects / limbs using this shader use */
	float4		pos			: POSITION;
	float4		colour		: COLOR;
	float2		texcoord	: TEXCOORD;
	/* This is a system value added by the input assembler stage on request; it should NOT be included in your vertex layout */
	uint		iId			: SV_INSTANCEID;
};

// Output from the vertex shader / input to the pixel shader
struct vs_out {
	float4 pos		: SV_POSITION;
	float4 colour	: COLOR0;
	float2 texcoord : TEXCOORD;
};


// Actual vertex shader - the function name here is the one you should provide to DX11 LOAD VERTEX SHADER
// along with the filename of this file.
vs_out VS(vs_in IN) {
	vs_out OUT;

	OUT.pos			= mul(IN.pos, Transform[IN.iId].WorldViewProjection);
	OUT.colour		= IN.colour;
	OUT.texcoord	= IN.texcoord;

    return OUT;
}

// Actual pixel shader - the function name here is the one you should provide to DX11 LOAD PIXEL SHADER
// along with the filename of this file.
float4 PS(vs_out IN) : SV_TARGET {
	float4 diffuse = DiffuseMap.Sample(DiffuseSampler, IN.texcoord);
	// Perform alpha test instead of alpha blending; if the alpha value of the highest texture mip level is 
	// below 0.5 we discard this fragment, otherwise we draw it as though it had full alpha.
	clip(DiffuseMap.SampleLevel(DiffuseSampler, IN.texcoord, 0).a - 0.8);
	return float4(diffuse.xyz, 1.0);
}
