Jump to content

  • Log In with Google      Sign In   
  • Create Account

Instancing my terrain patches


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 25 May 2012 - 05:02 AM

Hello again! I'm currently rendering my terrain as a bunch of patches. There are different LOD levels of the patch. Each LOD level has it's own index buffer. No vertex buffer is used, the planar position of the vertex is calculated with SV_VertexID and the height is then sampled from a texture. Below is the shader for this:

//======================================================================================//
// 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!

Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2852

Like
1Likes
Like

Posted 25 May 2012 - 05:52 AM

How about using a texture2darray? it can be indexed dynamically.

Best regards!

Edited by kauna, 25 May 2012 - 05:53 AM.


#3 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 26 May 2012 - 03:15 AM

Hmm, yeah I thought a little about that. But they are stored as ONE object in the application, am I right? Like so:
Texture2DDescription texture2DDescription = new Texture2DDescription();
            texture2DDescription.ArraySize = 8;

That means it has to be of dynamic usage and write to before each draw call. Isn't that kind of performance heavy, if I have say an array size of 8 and the height maps would be something like 256 * 256? But maybe it's worth the fact that it's 7 less draw calls if you fill a batch with 8 patches...? Thoughts? :D

Thanks in advance!

#4 kauna   Crossbones+   -  Reputation: 2852

Like
0Likes
Like

Posted 29 May 2012 - 12:35 AM

Yes you'll need to fill the textures before drawing, but the point is to fill only the parts which change. Check for toroidal updating.

Also, you may not need 8 levels of textures although you have 8 levels of LODs. Either you pack your heights inside one texture, or
If you don't mind wasting some texture space a you can create bigger texture which contain the height values of several lods.

If I remember correctly, I'm using 2 textures and LOD levels from 8km down to 32 or 64 meters.

Cheers!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS