//======================================================================================//
// Constant buffers //
//======================================================================================//
cbuffer Initial : register(b0) {
int NumCells;
float SliceSize;
}
cbuffer EveryFrame : register(b1) {
matrix World;
matrix View;
matrix Projection;
}
//======================================================================================//
// Input/Output structures //
//======================================================================================//
struct AppToVertex {
uint VertexID : SV_VERTEXID;
};
struct VertexToPixel {
float4 Position : SV_POSITION;
};
//======================================================================================//
// Shader resources //
//======================================================================================//
Texture2D HeightMap : register(t0);
//======================================================================================//
// Samplers //
//======================================================================================//
SamplerState HeightMapSampler : register(s0) {
Filter = MIN_MAG_MIP_LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
//======================================================================================//
// Helper function prototypes //
//======================================================================================//
float2 GetPosition(uint vertexID);
float GetHeight(float2 position);
//======================================================================================//
// Vertex Shader //
//======================================================================================//
VertexToPixel VS(in AppToVertex input) {
VertexToPixel output = (VertexToPixel)0;
// Calculate the location of this vertex
float2 p = GetPosition(input.VertexID);
// Calculate the height of this vertex
float h = GetHeight(p);
// Apply transform
output.Position = float4(p.x, h, p.y, 1);
output.Position = mul(output.Position, World);
output.Position = mul(output.Position, View);
output.Position = mul(output.Position, Projection);
return output;
}
//======================================================================================//
// Helper method for calculating the position of a specific vertex //
//======================================================================================//
float2 GetPosition(uint vertexID) {
int numVerts = NumCells + 1;
float x = (float)(vertexID % numVerts) * (SliceSize / NumCells);
float z = (float)(vertexID / numVerts) * (SliceSize / NumCells);
return float2(x, z);
}
//======================================================================================//
// Helper method for sampling the height map //
//======================================================================================//
float GetHeight(float2 position) {
// Calculate the texture coordinate
float u = position.x / NumCells;
float v = position.y / NumCells;
// Sample the height map
return HeightMap[float2(u, v)].r;
}
Now, I would like to instance patches that currently have the same LOD level and has the same materials (if that's necessary, better if that's not needed).
I can have a structured buffer that contains data about each patch (patch X location, patch Z location, materials etc) and use SV_InstanceID to fetch it. But my concern is the height map. I tried using a simple array, like so:
Texture2D HeightMap[8]
But you cant dynamically index an array using SV_InstanceID like you can do with the structured buffer. So does anyone know how I can do instead? Putting the height map in the structured buffer would be cool, but you can't really do that can you...? Is there any other way, like a texture buffer (is this what tbuffer is for?) or something like that?
Or maybe I can use a separate structured buffer like this:
StructuredBuffer<Texture2D> HeightMaps;
?
Any ideas are welcome!
Thanks in advance!