Sign in to follow this  

Using D3D11_QUERY_OCCLUSION

This topic is 1915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Has anyone used this for the purpose of occlusion culling and had it work for them?

I must be doing something wrong because it drops me from 70FPS (Frustum Culling only, 10,000 objects) to 1FPS and only half works (only one block works as an occlusion culler). If there is a better method for occlusion culling, please let me know. This appears to work well when used correctly, but I'm not sure how to correctly use it.... and I find Microsoft's reference guide to be useless for learning.


[source lang="cpp"] // Setup how the query functions
queryDesc.Query = D3D11_QUERY_OCCLUSION;
queryDesc.MiscFlags = 0;

// Create the query
m_D3D->GetDevice()->CreateQuery(&queryDesc, &pQuery);

// Go through all the models and render them only if they can be seen by the camera view.
for(index=0; index<modelCount; index++)
{

// Get the position and color of the object model at this index.
m_ModelList->GetData(index, positionX, positionY, positionZ, color);

// Texture number that holds the low resolution occlusion texture
texture = 4;

// Start the query
m_D3D->GetDeviceContext()->Begin(pQuery);

// Matrix translation
D3DXMatrixTranslation(&worldMatrix, positionX, positionY, positionZ);

// Render the object's occlusion texture
result = m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(texture), m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());

// End the query
m_D3D->GetDeviceContext()->End(pQuery);

// Get the data from the query and determine if object should be rendered
// Returns whether or not a object is in view. If 0, then the object is not in view.
while ( S_OK != m_D3D->GetDeviceContext()->GetData(pQuery, &queryData, sizeof(UINT64), 0 ) )
{
// If object is not in view
if (queryData == 0)
// Should not be rendered
renderModel = false;
// If object is in view
else
// Should be rendered
renderModel = true;
}

// Render the model if it was in view
if(renderModel)
{
// Move the model to the location it should be rendered at.
D3DXMatrixTranslation(&worldMatrix, positionX, positionY, positionZ);

// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
m_Model->Render(m_D3D->GetDeviceContext());

// Random texture thing (worked before this occlusion culling. now the culling texture takes over)
if (positionY == 0)
texture = 3;
else
if (positionY >= -16)
texture = 2;
else
texture = 1;

// Render the model using the light shader.
result = m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(texture), m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());

// Reset to the original world matrix.
m_D3D->GetWorldMatrix(worldMatrix);

// Since this model was rendered then increase the count for this frame.
renderCount++;
}
}
[/source] Edited by Kurask

Share this post


Link to post
Share on other sites
Hey there, I've been working on it here still.

I see what you mean about fetching results immediately, and I fixed that. I also now create the array of query objects prior to runtime. Both improved the performance, however, I'm still doing something wrong as it does not actually do any culling.

Here's my new code. The render count always shows up as 0, therefore nothing is being set as OK to render with the correct texture. However, all of the object are rendered with the 20x20 texture, so the first loop is working.

[source lang="cpp"] // Go through all the models
for(int index = 0; index < modelCount; index++)
{
// Get the position of the model
m_ModelList->GetData(index, positionX, positionY, positionZ, color);

// Leftovers
radius = 1.0f;

// Check if the model is in the frustum
renderModel = m_Frustum->CheckCube(positionX, positionY, positionZ, radius);

// If it can be seen...
if(renderModel)
{
// Move the model to the location it should be rendered at.
D3DXMatrixTranslation(&worldMatrix, positionX, positionY, positionZ);

// Prime the buffers
m_Model->Render(m_D3D->GetDeviceContext());

// Start the query
m_D3D->GetDeviceContext()->Begin(m_pPredicate[index]);

// Render the model to the world using the 20x20 texture
result = m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(2), m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());

// End the query
m_D3D->GetDeviceContext()->End(m_pPredicate[index]);

// Reset the world matrix
m_D3D->GetWorldMatrix(worldMatrix);
}
}

// Go through all the models
for (int index = 0; index < modelCount; index++)
{
// Stores the result of the query
bool renderM = 0;

// Get the result of the query and store it to renderM
m_D3D->GetDeviceContext()->GetData(m_pPredicate[index], &renderM, sizeof(BOOL), 0);

// If the query said the object was OK to render... (IS NOT WORKING AT ALL)
if (renderM == true)
{
// Reset the status to false
m_D3D->GetDeviceContext()->SetPredication(m_pPredicate[index], FALSE);

// Get the model information
m_ModelList->GetData(index, positionX, positionY, positionZ, color);

// Move the model to the right location
D3DXMatrixTranslation(&worldMatrix, positionX, positionY, positionZ);

// Prime the buffers
m_Model->Render(m_D3D->GetDeviceContext());

int texture;
if (positionY == 0)
texture = 3;
else
if (positionY >= -16)
texture = 2;
else
texture = 1;

// Render the model to the screen with the right texture
result = m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(texture), m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());

// Reset to the original world matrix.
m_D3D->GetWorldMatrix(worldMatrix);

// Since this model was rendered then increase the count for this frame.
renderCount++;
}
}[/source]


And now I have to ask, how is everyone else handling this? It doesn't appear to be a common question on the internet at all! Right now my testing is done with 1 model rendered in 10,000 different locations, but that will increase massively once I find a good method for occlusion. The FPS performance is already far less than I want for what I currently have.

Share this post


Link to post
Share on other sites
The reason you can't find much information is because not many people are doing it. Plenty have tried, and failed. It's really hard to make occlusion queries work for a general visibility system, due to the latency and batching problems. The more "en vogue" technique at the moment is software depth buffer rasterization + occlusion testing.

Share this post


Link to post
Share on other sites
Fair enough, I figured it wasn't that great of a way.

I'll look for another method then. In the end, I only plan on having maybe 5 different models and have them rendered thousands of times each. Edited by Kurask

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1350682551' post='4991944']
The reason you can't find much information is because not many people are doing it. Plenty have tried, and failed. It's really hard to make occlusion queries work for a general visibility system, due to the latency and batching problems.
[/quote]

+1; there is wisdom in this.

Occlusion queries are one of those features that look [b]great[/b] on paper, but when you actually start implementing something and dealing with all of the nasty edge cases you quickly realise that they're not exactly all they're cracked up to be.

What they [i]may[/i] be good for is pre-processing a map to determine visibility info, but at runtime there are just too many places where they can fail to give you a good result to make them of much utility for a general case solution.

Share this post


Link to post
Share on other sites
Sign in to follow this