Jump to content

  • Log In with Google      Sign In   
  • Create Account

batch draw!


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

#1 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 December 2012 - 04:06 PM

We know put all the vertices together and call drawXXX() can draw all the triangles.
But if each triangles have different textures,what should i do?DX9 doesn't has texture array.
And if we use pixel shader ,i know use pixel shader can sample texture.But pixel shader 2.0 only has 16 sampler.
I think it is use for multiple texture.But how to pass texture into pixel shader?Could anybody give me a example or a idea?
Thank you!

Edited by AlanWu, 09 December 2012 - 07:35 PM.


Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3883

Like
0Likes
Like

Posted 09 December 2012 - 04:23 PM

We know put all the vertices together and call drawXXX() can draw all the triangles.
But if each triangles have different ,what should i do?DX9 doesn't has texture array.
And if we use pixel shader ,i know use pixel shader can sample texture.But pixel shader 2.0 only has 16 sampler.
I think it is use for multiple texture.But how to pass texture into pixel shader?Could anybody give me a example or a idea?
Thank you!


diffrent...? i'm guessing you mean textures. if so, you could alternatively create a super texture of all the sprites, and adjust the uv's of the object's appropriately.

Edited by slicer4ever, 09 December 2012 - 04:43 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 Steve_Segreto   Crossbones+   -  Reputation: 1530

Like
2Likes
Like

Posted 09 December 2012 - 04:41 PM

The basic idea for a texture atlas (or texture array or super texture) for 2-d drawing is to vary the (u,v) values to represent the different tiles.

Imagine you have a 2x2 texture atlas:

ab
cd

If you want to describe a textured quad for each one the u,v coordinates are:

textured quad showing tile a. SetTexture(TextureAtlas), U,V = (0, 0) to (0.5,05)
textured quad showing tile b. No more need to SetTexture. U,V = (0.5,0) to (1.0,0)
textured quad showing tile c. No more need to SetTexture. U,V = (0,0.5) to (0,1.0)
textured quad showing tile d. No more need to SetTexture. U,V = (0.5,0.5) to (1.0,1.0)

Hope this helps with the concept.

There are a few different choices for D3D9 implementation. Use Google and you should be able to find a few.

#4 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 December 2012 - 07:37 PM


We know put all the vertices together and call drawXXX() can draw all the triangles.
But if each triangles have different ,what should i do?DX9 doesn't has texture array.
And if we use pixel shader ,i know use pixel shader can sample texture.But pixel shader 2.0 only has 16 sampler.
I think it is use for multiple texture.But how to pass texture into pixel shader?Could anybody give me a example or a idea?
Thank you!


diffrent...? i'm guessing you mean textures. if so, you could alternatively create a super texture of all the sprites, and adjust the uv's of the object's appropriately.

yes,you are right,i have changed it just now.
thank you!

Edited by AlanWu, 09 December 2012 - 07:39 PM.


#5 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 December 2012 - 07:39 PM

The basic idea for a texture atlas (or texture array or super texture) for 2-d drawing is to vary the (u,v) values to represent the different tiles.

Imagine you have a 2x2 texture atlas:

ab
cd

If you want to describe a textured quad for each one the u,v coordinates are:

textured quad showing tile a. SetTexture(TextureAtlas), U,V = (0, 0) to (0.5,05)
textured quad showing tile b. No more need to SetTexture. U,V = (0.5,0) to (1.0,0)
textured quad showing tile c. No more need to SetTexture. U,V = (0,0.5) to (0,1.0)
textured quad showing tile d. No more need to SetTexture. U,V = (0.5,0.5) to (1.0,1.0)

Hope this helps with the concept.

There are a few different choices for D3D9 implementation. Use Google and you should be able to find a few.


i have a idea now,thank you very much!
will the method be slow?if so,do you have any other method?
Thank you for your helping!

#6 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 December 2012 - 08:02 PM

The basic idea for a texture atlas (or texture array or super texture) for 2-d drawing is to vary the (u,v) values to represent the different tiles.

Imagine you have a 2x2 texture atlas:

ab
cd

If you want to describe a textured quad for each one the u,v coordinates are:

textured quad showing tile a. SetTexture(TextureAtlas), U,V = (0, 0) to (0.5,05)
textured quad showing tile b. No more need to SetTexture. U,V = (0.5,0) to (1.0,0)
textured quad showing tile c. No more need to SetTexture. U,V = (0,0.5) to (0,1.0)
textured quad showing tile d. No more need to SetTexture. U,V = (0.5,0.5) to (1.0,1.0)

Hope this helps with the concept.

There are a few different choices for D3D9 implementation. Use Google and you should be able to find a few.


well,i think i can make a texture atlas by myself,and not let my program make it every times before it use.
But i have some problems here,if every textures have a different size,what should i do?
if i put all the texture together and it is bigger than texture max size which video-card support?what should i do?
some people said i can use pixel shader to draw different textures,how to do that?
thank you!

Edited by AlanWu, 09 December 2012 - 08:08 PM.


#7 C0lumbo   Crossbones+   -  Reputation: 2258

Like
0Likes
Like

Posted 10 December 2012 - 02:01 AM

"well,i think i can make a texture atlas by myself,and not let my program make it every times before it use." - You probably should automate this process, else you might get very frustrated when your images change size, or your decide to add a little border around each image, or you change the size of an atlas page, etc.

"But i have some problems here,if every textures have a different size,what should i do?" - Packing different sized textures optimally is a very hard problem, it's a variant of the bin packing problem and is basically unsolvable. There are simple algorithms which produce good results, but you might be best using an existing packing tool rather than write your own.

"if i put all the texture together and it is bigger than texture max size which video-card support?what should i do?" - If you're doing it offline (i.e. you're running your atlas step before you run the game), then you're best off just choosing a max size like 1024x1024 which every reasonable card would support. You will then have to handle the possibility that your textures will end up spread across multiple pages.

"some people said i can use pixel shader to draw different textures,how to do that?" - This doesn't sound like a good idea to me. If your textures are all different sizes, then you'd need to use different samplers and select in your pixel shader, tbh, you'd be better off not batching at all. If your textures are all the same size and all your target hardware support array textures (unlikely), then that might be a reasonable option, your pixel shader would then just be choosing a plane of a single array texture which would be reasonably efficient.

#8 mk1x86   Members   -  Reputation: 442

Like
0Likes
Like

Posted 10 December 2012 - 02:37 AM

If your atlas exceeds the supported texture size you must split it into two atlas textures. You should gather sprites, that are likely to be close to each other in game, in one atlas. If you have different themes (e.g. desert & ice) you should place all desert sprites in one atlas and all ice sprites into the other.

As for shaders, you can draw different textures, but you won't get any performance gain from that as you still need to load both textures to vram. On "how" to do that I suggest getting a book on shader programming since this isn't explained in a few sentences and - depending on what you want do - requires a fair amount of advanced mathematics.

#9 NightCreature83   Crossbones+   -  Reputation: 2822

Like
1Likes
Like

Posted 10 December 2012 - 02:47 AM

"But i have some problems here,if every textures have a different size,what should i do?" - Packing different sized textures optimally is a very hard problem, it's a variant of the bin packing problem and is basically unsolvable. There are simple algorithms which produce good results, but you might be best using an existing packing tool rather than write your own.

Actually it is not that hard to solve if you allow for a few errors in the process. You should always pack square textures on area size, once you have that sorted list fit the biggest one into the texture and split it the original texture on the inserted textures biggest dimension. Now check if the next texture fit into the biggest square that doesn't hold a texture yet. If it doesn't output an error and increase the atlas texture size and start again. If it does sub divide that square again as before and continue with the next square. Here is the source article I used when I had to implement this for a game. Remember ordering on area size is important to get an efficient subdivision of the texture.

This problem gets harder when you have to pack a texture with the atlas for a model and flattened out geometry although the same process should work as well, you might need a different form than a square to sub divide on though.

Edited by NightCreature83, 10 December 2012 - 02:51 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 December 2012 - 06:13 AM

thanks everybody
now i only have a few problem,could you continue helping me?
i think i have two ways to go:
1.make a texture atlas when i packing game data,divide different themes texture into different texture atlas.So i don't need to do anythings when i run game.But i need to call SetTexture() more than once if it has different themes texture in my game.
2.make a texture atlas which it need to use before run game,it may take some times for loading but it only need to call SetTexture() for once.
And i have another problem,we know texture coordinate is float,but texture is int(pixel),if i want to draw a texture from (100,100) to (200,200),but the size of texture atlas is 1024x1024,and 100/1024 is not a limited decimal ,what should i do?Should i change the size of texture atlas to 1000x1000?And do i have other ways?And do you have some detail articles about texture atlas?i want to learn it.
Thank you guys very much!
Oh,i start some topic here ,i hope you can go to have a look and reply.
vertex rhw and camera in 2D
about Font
thanks again!

#11 NightCreature83   Crossbones+   -  Reputation: 2822

Like
0Likes
Like

Posted 10 December 2012 - 07:34 AM

Texture coordinates don't have to be 1,2,3..100 they can be any floating point value, 0.0025 is a valid texture coordinate. The GPU takes care of transforming your texture coordinate into the correct pixels in the actual texture image. The normal range is 0...1 if you want to show the whole texture, but if you go over one it will repeat on itself if the wrap state is setup that way.

When using atlas textures the texture coordinates of your objects need to be scaled to the range the texture takes in the atlas texture, and you don't store this as pixels but as actual coordinates to make it easier on yourself during drawing.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#12 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 December 2012 - 04:06 PM

Texture coordinates don't have to be 1,2,3..100 they can be any floating point value, 0.0025 is a valid texture coordinate. The GPU takes care of transforming your texture coordinate into the correct pixels in the actual texture image. The normal range is 0...1 if you want to show the whole texture, but if you go over one it will repeat on itself if the wrap state is setup that way.

When using atlas textures the texture coordinates of your objects need to be scaled to the range the texture takes in the atlas texture, and you don't store this as pixels but as actual coordinates to make it easier on yourself during drawing.

Well,i got a good idea for two ways,finally idea:
When i make texture atlas put same theme's texture as much as possible together,just don't call SetTexture() as much as possible.But if really need other theme's texture,make a texture atlas before drawing,i think it will be very quick and i only need to call SetTexture() for twice.

yes,i know that, i know we usually use from 0 to 1,i mean..well,for a example
i have a texture ,size is 369x153,i called it A.
i have another texture ,size is 694x237,i called it B.
Well,if i make a texture atlas,size is 1024x1024 and i want to put these two texture like that:
(0,1024),(0,0)┏━━┓(1024,1024),(1,0)
┃A B ┃
┃┃
(0,0),(0,1)┗━━┛(1024,0),(1,1)
first coordinate is pixel coordinate,second is texture coordinate
well,that is the layout,but what is the texture coordinate for A?
should it like that?
left-top:(0,0)
right-top:(369/1024,0) //369 divide by 1024,because the width of A is 369,and the width of texture atlas is 1024
left-bottom:(0,153/1024) //153 divide by 1024,because the height of A is 153,and the height of texture atlas is 1024
right-bottom:(369/1024,153/1024) //same
i think it may be like that,but it has a problem,369 divide by 1024 is a recurring decimal ,so i think the GPU can't get a right pixel value for the width of A,and other is same.
So,should i change the size of texture atlas to 1000,then is won't be a recurring decimal 0.369(369/1000),should i do that?Please tell me.
And i have another question,how many decimal places could pass to GPU,if i pass 0.0025 to GPU,will it make it to 2 decimal places?Does it decide by float?
Thank you very much!

Edited by AlanWu, 10 December 2012 - 04:10 PM.


#13 NightCreature83   Crossbones+   -  Reputation: 2822

Like
0Likes
Like

Posted 11 December 2012 - 02:53 AM

Right I get your question now. Each pixel in a texture basically corresponds to 1/width for the horizontal axis and 1/height for the vertical axis. So if you know the top left and size of a texture you can easily calculate where the next free space is. If you read the source article I posted you see they encode the position and size of the texture in the tree structure they create.

You mapped your texture wrong though in windows pixel and coordinates all start from the left top so (0,0) = (0px,0px), (1, 1) = (1024px, 1024px). Also in DX9 you need to take in to account that you need to squeeze the texture by half a texel coordinate so you won't sample into the neighbouring texture. You shouldn't worry about recurring decimals because as I said your going to have to subtract half a texel from the original coordinates anyway (basically subtract 0.5/width from u and 0.5/height from v). See this for texture coordinate mapping in DX9 and this Nvidia sample for more on atlas texturing
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#14 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 11 December 2012 - 04:47 PM

Right I get your question now. Each pixel in a texture basically corresponds to 1/width for the horizontal axis and 1/height for the vertical axis. So if you know the top left and size of a texture you can easily calculate where the next free space is. If you read the source article I posted you see they encode the position and size of the texture in the tree structure they create.

You mapped your texture wrong though in windows pixel and coordinates all start from the left top so (0,0) = (0px,0px), (1, 1) = (1024px, 1024px). Also in DX9 you need to take in to account that you need to squeeze the texture by half a texel coordinate so you won't sample into the neighbouring texture. You shouldn't worry about recurring decimals because as I said your going to have to subtract half a texel from the original coordinates anyway (basically subtract 0.5/width from u and 0.5/height from v). See this for texture coordinate mapping in DX9 and this Nvidia sample for more on atlas texturing


sorry,i don't exactly understand,my english is not very good
For this sentence ,your going to have to subtract half a texel from the original coordinates anyway (basically subtract 0.5/width from u and 0.5/height from v).
do you mean subtract-0.5 for each x and each y?the article said we just need to subtract -0.5 for each x and each y.
could you give me a example?that will help me understand,thank you!
And ,for this sentence:you need to squeeze the texture by half a texel coordinate.
how to do that?
thank you!

#15 NightCreature83   Crossbones+   -  Reputation: 2822

Like
0Likes
Like

Posted 12 December 2012 - 03:06 AM

Read this article it's been a long time since I implemented this and don't remember all of the details anymore. The DX sample is adjusting the vertex coordinates instead of thet texture coordinates, it's better to fix the texture coordinates so they are in the range [0.5/width, 1 - 0.5 / width] for u and [0.5 / height, 1 - 0.5 / height] for v.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#16 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 12 December 2012 - 05:48 PM

Read this article it's been a long time since I implemented this and don't remember all of the details anymore. The DX sample is adjusting the vertex coordinates instead of thet texture coordinates, it's better to fix the texture coordinates so they are in the range [0.5/width, 1 - 0.5 / width] for u and [0.5 / height, 1 - 0.5 / height] for v.


ok..thank you
I am sorry,but i can't understand it at all,my English is not good enough, i can't read this article.But i still don't understand and it said"Most applications, however, use texture coordinates ranging from zero to one,inclusive, nonetheless.".
The things which i don't understand is 'models need to use u-coordinates in the range [.5/width, 1-.5/width] and v-coordinates in the range [.5/height, 1-.5/height].'why do we need to do that?

#17 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 12 December 2012 - 07:16 PM

left-top:(0,0)
right-top:(369.0f/1000,0) //369 divide by 1000,because the width of A is 369,and the width of texture atlas is 1000
left-bottom:(0,153.0f/1000) //153 divide by 1000,because the height of A is 153,and the height of texture atlas is 1000
right-bottom:(369.0f/1000,153.0f/1000) //same

i do this,it works,does it wrong?

#18 Steve_Segreto   Crossbones+   -  Reputation: 1530

Like
0Likes
Like

Posted 13 December 2012 - 01:11 AM

The things which i don't understand is 'models need to use u-coordinates in the range [.5/width, 1-.5/width] and v-coordinates in the range [.5/height, 1-.5/height].'why do we need to do that?


I think that might just be for displaying a fullscreen quad and is a way of taking into account the texel-to-pixel offset in DX9 and below.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx

#19 AlanWu   Members   -  Reputation: 191

Like
0Likes
Like

Posted 13 December 2012 - 02:56 PM


The things which i don't understand is 'models need to use u-coordinates in the range [.5/width, 1-.5/width] and v-coordinates in the range [.5/height, 1-.5/height].'why do we need to do that?


I think that might just be for displaying a fullscreen quad and is a way of taking into account the texel-to-pixel offset in DX9 and below.

http://msdn.microsof...0(v=vs.85).aspx


ok,thank you!
Thanks everyone!
i think it is no problem now.
And i am writing texture atlas editor.

#20 Steve_Segreto   Crossbones+   -  Reputation: 1530

Like
0Likes
Like

Posted 13 December 2012 - 03:34 PM


The things which i don't understand is 'models need to use u-coordinates in the range [.5/width, 1-.5/width] and v-coordinates in the range [.5/height, 1-.5/height].'why do we need to do that?


I think that might just be for displaying a fullscreen quad and is a way of taking into account the texel-to-pixel offset in DX9 and below.

http://msdn.microsof...0(v=vs.85).aspx


Let me amend this and say that anytime you programmatically generate u,v coordinates you have to take into account the texel-to-pixel offset when using DX9 and below, not just for full-screen quads.




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