Jump to content

  • Log In with Google      Sign In   
  • Create Account


Grass Rendering with DirectX 11 not working properly

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

#1 BlurEffect   Members   -  Reputation: 159

Like
0Likes
Like

Posted 04 June 2014 - 10:48 AM

Hi guys,

 

I'm trying to get grass rendering to work with DirectX 11 and I'm pretty much following this GPU Gems article: http://http.developer.nvidia.com/GPUGems/gpugems_ch07.html. Basically, at the moment I have a mesh consisting of three interleaved quads that have grass textures with transparent parts on them. I'm animating these grass objects by moving the upper vertices of the quads in the vertex shader. The article says about the grass objects: "sort them back-to-front at runtime, use alpha blending, and enable z-testing/writing in the draw call". I thought I had this set up correctly but something isn't as I get some visual errors when rendering the animated grass. More precisely, when two quads of adjacent grass objects intersect, one of them gets hidden rather than being properly blended. Also when I generally render the grass objects, some parts (on the side opposite to the camera) are not visible although the occluding quad  should have transparent parts. I also don't get why some transparent parts of the textures are rendered in black instead of being actually transparent, they occlude other stuff in the application (like the grid you can see in the images below, although the grid is rendered after the grass). It's a bit difficult for me to explain, so I'm attaching three pictures to this post:

 

Grass1.png

This image shows how the three quads making up the mesh of my grass objects are interleaved.

 

Grass2.png

This image shows a single grass object. The more distant parts are not visible, it's pretty much just showing half the object.

 

Grass3.png

This image shows how some parts of one grass object are occluded by the black parts of another one.

 

Here is the code, in which I set up the blending state and the depth-stencil-state for rendering of the grass.

D3D11_DEPTH_STENCIL_DESC m_grassStencilDesc;
ZeroMemory( &m_grassStencilDesc, sizeof( D3D11_DEPTH_STENCIL_DESC ) );

// Set up the description of the stencil state.
m_grassStencilDesc.DepthEnable = true;
m_grassStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
m_grassStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; 

m_grassStencilDesc.StencilEnable = true;
m_grassStencilDesc.StencilReadMask = 0xFF;
m_grassStencilDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing.
m_grassStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
m_grassStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing.
m_grassStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
m_grassStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
m_grassStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

// Create the depth stencil state.
m_pD3d11Device -> CreateDepthStencilState(&m_grassStencilDesc, &m_grassStencilState);


D3D11_BLEND_DESC m_grassBlendingStateDesc;
ZeroMemory(&m_grassBlendingStateDesc, sizeof(D3D11_BLEND_DESC));

// Create an alpha enabled blend state description.
m_grassBlendingStateDesc.RenderTarget[0].BlendEnable = TRUE;
m_grassBlendingStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;//D3D11_BLEND_ONE;
m_grassBlendingStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
m_grassBlendingStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
m_grassBlendingStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
m_grassBlendingStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
m_grassBlendingStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
m_grassBlendingStateDesc.RenderTarget[0].RenderTargetWriteMask = 0x0f;

// Create the blend state using the description.
m_pD3d11Device -> CreateBlendState( &m_grassBlendingStateDesc, &m_grassBlendingState );

I am sorting the grass object back to front, as the article suggests. To do this I compare the distance from the centre of each grass object to the camera position. When the objects are animated the quad of an object that is actually farther away from the camera might be moved towards the camera in a fashion that it is now actually placed in front of a grass object that is closer to the camera. Might this actually be the problem? Should I sort the single quads instead of the grass objects? But if so, how would I determine which quad is closer to the camera, as all three have their centre at the same position. Do you generally have any idea what's going wrong, maybe an error in the code above? I hope you guys can help me, thank you very much in advance.



Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3193

Like
0Likes
Like

Posted 04 June 2014 - 11:48 AM

I'm not sure why that article suggests enabling z-write if you are already sorting back-to-front. Disabling it will make the grass look better, although you'll still have some more subtle alpha-blending order issues.

 

For your issue with single grass objects, you may need to break your 3 intersecting grass quads into 6 pieces (that all meet in the "center" of the grass object). Then those would be drawn in a back-to-front order.

 

As for the issue with adjacent grass objects intersecting, I would suggest placing them further apart (like the images in the article you linked)


Edited by phil_t, 04 June 2014 - 11:48 AM.


#3 BlurEffect   Members   -  Reputation: 159

Like
0Likes
Like

Posted 04 June 2014 - 12:27 PM

Thank you very much for your answer. I actually tried disabling z-write before but as you mentioned there are still some order issues as the image below shows. However, I don't get the other problems with intersections or parts of the grass object not being drawn. I guess in order to solve the issue present in the image below (it's a bit difficult to spot though), I would have to order the quads back to front and not the grass objects as a whole. At the moment with all quads centred in the same spot, I have no idea how you could sort them properly. However, as I read your post, I thought that I could split up each quad into two quads (just as you suggested to solve one of the problems), who would then all have their centre in different spots. I will try that out. If you or someone else know of other things I could try, it would be great if you could share them, too.

Attached Thumbnails

  • Grass4.png

Edited by BlurEffect, 04 June 2014 - 12:28 PM.


#4 phil_t   Crossbones+   -  Reputation: 3193

Like
0Likes
Like

Posted 04 June 2014 - 12:48 PM


At the moment with all quads centred in the same spot, I have no idea how you could sort them properly.

 

I think you could avoid complex sorting with some clever tricks. Assuming all your 6-quad grass objects are oriented the same way in the world, I think there are probably only 6 different "sets of orders" that you would ever need (maybe less), depending on camera angle. So you could have 6 different index buffers that define the different order of the grass quads within a bunch, and you choose one of them based on camera angle. I could diagram if I'm not explaining clearly.

 

Also, to avoid all these sorting problems you could use alpha-test instead of alpha blending. The upside is that you no longer need to sort, and you no longer need to render your grass after all other objects, as the depth buffer will be correct. The downside is that visual quality will suffer - your grass will have jaggy edges.



#5 BlurEffect   Members   -  Reputation: 159

Like
0Likes
Like

Posted 04 June 2014 - 01:45 PM

How would I actually use alpha-testing in DirectX 11. I couldn't really find anything.

 

Edit: Apparently you can no longer enable Alpha-Testing in DirectX 11 but you have to do it manually in a shader by discarding pixels with alpha values below a user-defined threshold. I tried it, and yeah the edges become quite jaggy, but as the camera in the game will be at a bit of a distance from the grass at all times, I might get away with it. It's still annoying though that I can't really get it to work as it was described in that article.


Edited by BlurEffect, 04 June 2014 - 02:01 PM.


#6 phil_t   Crossbones+   -  Reputation: 3193

Like
0Likes
Like

Posted 04 June 2014 - 02:52 PM


It's still annoying though that I can't really get it to work as it was described in that article.

 

What about the other suggestions I made? i.e.:

 


I think you could avoid complex sorting with some clever tricks. Assuming all your 6-quad grass objects are oriented the same way in the world, I think there are probably only 6 different "sets of orders" that you would ever need (maybe less), depending on camera angle. So you could have 6 different index buffers that define the different order of the grass quads within a bunch, and you choose one of them based on camera angle. I could diagram if I'm not explaining clearly.


#7 BlurEffect   Members   -  Reputation: 159

Like
0Likes
Like

Posted 05 June 2014 - 04:51 AM

So, I could solve the issue that I had for single grass objects by using single quads and sorting them back to front (instead of just sorting the grass objects). However, I still couldn't figure out any working solution to prevent visual errors as soon as quads intersect. But that's not really a problem of the sorting as during the intersections there is one part of a specific quad in front of the other one while the rest of it is behind it. So you simply can't sort that properly. I don't understand why that alpha-blending is not working as it seems to do in the article. Or maybe there are really no intersections at all in the article approach...



#8 kauna   Crossbones+   -  Reputation: 2156

Like
0Likes
Like

Posted 05 June 2014 - 06:40 AM

As mentionned earlier, try alpha testing : use clip / discard in your shader to clip at certain alpha threshold. In the same time, enable depth testing and depth writes. 

 

That is the easiest solution which will produce output without visual errors. The edges of the grass blades will be more jagged, but it may or may not be a concern - some antialiasing techniques might improve the image quality.

 

You may also look in to OIT (order independent transparency) in order to draw the grass blades with alpha blending. There is plenty of research about OIT, but it isn't the easiest subject. 

 

The problem with the traditional alpha blending is that all the surfaces must be draw in back to front order, and it is very difficult to produce a perfect ordering with a triangle soup - and reordering the triangles with a modern GPU is not a good idea. Some solutions sort the objects in back to front order and with a good geometry (such as a screen aligned quad) the visual errors are pretty low. 

 

Cheers!


Edited by kauna, 05 June 2014 - 06:42 AM.


#9 tonemgub   Members   -  Reputation: 617

Like
0Likes
Like

Posted 05 June 2014 - 09:26 AM

This article gave me an idea: http://www.sjbaker.org/steve/omniv/alpha_sorting.html

 

Your case falls into the "Disabling Z-write for Translucent Polygons.", where it says: "If all the translucent polygons have the same colour, then this does actually work".

 

I think the steps required to get it working properly are:

1) enable z-writing and z-testing; clear the z-buffer

2) draw all of your non-grass objects (like the blue grid)

3) disable z-writing, but leave z-testing on

4) draw all of your grass objects in back-to-front order with alpha-blending, as described in the GPU Gems article

 

AFAIKT, the point that the GPU Gems article is trying to make is that you do not need perfect order-independent transparency in order to draw the grass. Order-independent alpha blending would induce a huge performance hit. You're over-analysing the individual grass mesh, but have you tried maybe adding 100 to 1000 of them to your scenery? If that looks ok, then you should stick with it.


Edited by tonemgub, 05 June 2014 - 09:29 AM.


#10 BlurEffect   Members   -  Reputation: 159

Like
0Likes
Like

Posted 07 June 2014 - 11:30 AM

I'm using alpha testing for now and will see how it works within the actual game (in my test application it seems to work and look fine). Depending on that I might consider some of the other advice given. Thanks guys!







PARTNERS