Jump to content

  • Log In with Google      Sign In   
  • Create Account

Instancing : Different textures for instances


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
5 replies to this topic

#1 Yann ALET   Members   -  Reputation: 224

Like
0Likes
Like

Posted 10 September 2013 - 06:20 PM

Hi there,
 
I am using instancing to draw a very large amount of cubes in a voxel world (yes another one ... ^_^). So far, all cubes used the same texture, but now i want to use different ones when i build the world.
All my textures are in a unique atlas sent to the GPU i sample from in the pixel shader.
So my question is this, performace wise, what is the best approach to display instanced cubes with different textures ?
With my limited experience, i see only two ways :
1- Instead of calling DrawIndexInstanced once for my 2 million cubes, i call it multiple times for each types of cubes (cubes with different textures).
2- I still call it once, but add a in the instance data the index of the texture in the atas, and to the UV lookup directly in the shader.
3- ?
 
I am tempted to go with option 2, but i fear doing the texture lookup so often is going to hurt performances real bad in the end.
 
Any suggestions ?
 
Thanks :-)


Sponsor:

#2 Pink Horror   Members   -  Reputation: 1228

Like
0Likes
Like

Posted 10 September 2013 - 06:29 PM

If all the textures are already in an atlas, aren't all cubes already using the same texture?



#3 Yann ALET   Members   -  Reputation: 224

Like
0Likes
Like

Posted 10 September 2013 - 06:50 PM

Well they all have access to the full atlas which contains all possible textures for a cube, but i still need set UV coordinates for each vertex of my cube.

I'll make things easier (because i am terrible at explaining things) by submitting my atlas and my code.

 

Here is the old atlas i was using :

 

QSk9xWA.png

 

And i initialized vertices of my unique cube type like such :

float oneQuarter = (float)1/3;
float twoQuarter = (float)2/3;
Vertex::Basic32 vertices[] =
	{
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 1) },     // 0 - front face  - bottom left
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },            // 1 - left face   - bottom right
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 0) },            // 2 - bottom face - top left -> clone

		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(twoQuarter, 0) },     // 3 - front face  - top left
		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },            // 4 - left face   - top right
		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },     // 5 - top face    - bottom left

		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 0) },            // 6 - front face  - top right
		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },     // 7 - right face  - top left
		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },     // 8 - top face    - bottom right

		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1, 1) },			  // 9 - front face  - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 10 - right face  - bottom left
		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 11 - bottom face - top right

		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 1) },    // 12 - left face   - bottom left
		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 1) },           // 13 - back face   - bottom right
		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(0, 1) },           // 14 - bottom face - bottom left

		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 15 - left face   - top left
		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(oneQuarter, 0) },    // 16 - top face    - top left  -> clone
		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(1, 0) },			  // 17 - back face   - top right

		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 0) },           // 18 - right face  - top right
		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(twoQuarter, 0) },    // 19 - top face    - top right -> clone
		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 0) },    // 20 - back face   - top left 

		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(1, 1) },           // 21 - right face  - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(oneQuarter, 1) },    // 22 - bottom face - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(twoQuarter, 1) },    // 23 - back face   - bottom left
	};	

Now, i have added a new texture to my atlas :

 

R8WnCgJ.png

 

So, i changed the above code to calculate UV based on texture index in the atlas (maybe that's not the right approach, i don't know :s).

void GeometryBuilder::MakeCube(Geometry& box, int bottomTextureIndex, int topTextureIndex, int sideTextureIndex)
{
	int numberOfTextures = 4;
	float textureWidth = 1.0f / numberOfTextures;

	Vertex::Basic32 vertices[] =
	{
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 1) },     // 0 - front face  - bottom left
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },            // 1 - left face   - bottom right
		{ XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 0) },            // 2 - bottom face - top left -> clone

		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * sideTextureIndex, 0) },     // 3 - front face  - top left
		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 4 - left face   - top right
		{ XMFLOAT3(-0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 1) },     // 5 - top face    - bottom left

		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },            // 6 - front face  - top right
		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },     // 7 - right face  - top left
		{ XMFLOAT3(+0.5f, +0.5f, -0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 1) },     // 8 - top face    - bottom right

		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },			  // 9 - front face  - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 10 - right face  - bottom left
		{ XMFLOAT3(+0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 0) },    // 11 - bottom face - top right

		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 12 - left face   - bottom left
		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 13 - back face   - bottom right
		{ XMFLOAT3(-0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex), 1) },           // 14 - bottom face - bottom left

		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 15 - left face   - top left
		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex), 0) },    // 16 - top face    - top left  -> clone
		{ XMFLOAT3(-0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },			  // 17 - back face   - top right

		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 0) },           // 18 - right face  - top right
		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, +1.0f, 0.0f), XMFLOAT2(textureWidth * (topTextureIndex + 1), 0) },    // 19 - top face    - top right -> clone
		{ XMFLOAT3(+0.5f, +0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 0) },    // 20 - back face   - top left 

		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(+1.0f, 0.0f, 0.0f), XMFLOAT2(textureWidth * (sideTextureIndex + 1), 1) },           // 21 - right face  - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f), XMFLOAT2(textureWidth * (bottomTextureIndex + 1), 1) },    // 22 - bottom face - bottom right
		{ XMFLOAT3(+0.5f, -0.5f, +0.5f), XMFLOAT3(0.0f, 0.0f, +1.0f), XMFLOAT2(textureWidth * (sideTextureIndex), 1) },    // 23 - back face   - bottom left
	};

// other stuff...	
}

This works, but forces me to call MakeCube for each type of cubes, and call DrawIndexedInstanced for each one of those.


Edited by Yann ALET, 10 September 2013 - 06:52 PM.


#4 eppo   Crossbones+   -  Reputation: 2621

Like
4Likes
Like

Posted 11 September 2013 - 08:54 AM

- Pass uv-offsets for different cubes as instance-data and add them to the base-uvs in the shader.

- If all cells in the texture are of equal size you can load them into a texture array and pass an array-index as instance data.

 

The second method has the advantage that all the cutting can be done either offline or during load time and it avoids filtering artifacts around cell borders.


Edited by eppo, 11 September 2013 - 09:00 AM.


#5 Yann ALET   Members   -  Reputation: 224

Like
1Likes
Like

Posted 11 September 2013 - 10:00 AM

Hi eppo !

Actually i implemented you 1st approach yesterday night and it works fine (but with artefects on edges as you mentionned).

A friend just told me 5 minutes ago about texture arrays which is EXACTLY what i need as you also suggested, so i'll implement that tonight, thank you so much !



#6 Norman Barrows   Crossbones+   -  Reputation: 2308

Like
0Likes
Like

Posted 11 September 2013 - 01:22 PM


- If all cells in the texture are of equal size you can load them into a texture array and pass an array-index as instance data.

 

+2 for that one!

 

 

oh, wait!  it won't do it!.....   ohmy.png


Edited by Norman Barrows, 11 September 2013 - 01:23 PM.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

 

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

 

 





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