Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[d3d9] Disable instancing for non-indexed draws #3158

Merged
merged 1 commit into from
Jan 13, 2023

Conversation

K0bin
Copy link
Collaborator

@K0bin K0bin commented Dec 31, 2022

Fixes #2473

https://learn.microsoft.com/en-us/windows/win32/direct3d9/efficiently-drawing-multiple-instances-of-geometry#drawing-non-indexed-geometry

The docs say the following about instancing with non-indexed draws:

This technique is not supported by hardware acceleration on any device. It is only supported by software vertex processing and will work only with vs_3_0 shaders.

It also seems to work differently in the non-indexed example with seemingly no way to set the instance count. On the other hand they also use DrawIndexedPrimitive in the example for non-indexed draws, so I don't really understand it.

The best I can deduce is that instancing is not supported for non-indexed draws. This probably needs to be tested on Windows.

@K0bin K0bin requested a review from misyltoad December 31, 2022 00:50
@K0bin K0bin added the d3d9 label Dec 31, 2022
@K0bin K0bin force-pushed the 40-billion-points branch 2 times, most recently from 73c936b to 148dedd Compare December 31, 2022 14:46
@misyltoad
Copy link
Collaborator

misyltoad commented Dec 31, 2022

@K0bin
In the MSDN article you mention, it is refering to D3DSTREAMSOURCE_INDEXEDDATA, not whether there is an index buffer used.

Look at the two examples, they both call SetIndices and DrawIndexedPrimitive but one does not set D3DSTREAMSOURCE_INDEXEDDATA.

Indexed Geometry Performance Comparison

if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
    // Set up the geometry data stream
    pd3dDevice->SetStreamSourceFreq(0,
                (D3DSTREAMSOURCE_INDEXEDDATA | g_numInstancesToDraw));
    pd3dDevice->SetStreamSource(0, g_VB_Geometry, 0,
                D3DXGetDeclVertexSize( g_VBDecl_Geometry, 0 ));

    // Set up the instance data stream
    pd3dDevice->SetStreamSourceFreq(1,
                (D3DSTREAMSOURCE_INSTANCEDATA | 1));
    pd3dDevice->SetStreamSource(1, g_VB_InstanceData, 0, 
                D3DXGetDeclVertexSize( g_VBDecl_InstanceData, 1 ));

    pd3dDevice->SetVertexDeclaration( ... );
    pd3dDevice->SetVertexShader( ... );
    pd3dDevice->SetIndices( ... );

    pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 
                g_dwNumVertices, 0, g_dwNumIndices/3 );
    
    pd3dDevice->EndScene();
}

Non-Indexed Geometry Performance Comparison

if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
    // Set the divider
    pd3dDevice->SetStreamSourceFreq(0, 1);
    pd3dDevice->SetStreamSource(0, g_VB_Geometry, 0,
                D3DXGetDeclVertexSize( g_VBDecl_Geometry, 0 ));

    // Set up the instance data stream
    pd3dDevice->SetStreamSourceFreq(1, verticesPerInstance));
    pd3dDevice->SetStreamSource(1, g_VB_InstanceData, 0, 
                D3DXGetDeclVertexSize( g_VBDecl_InstanceData, 1 ));

    pd3dDevice->SetVertexDeclaration( ... );
    pd3dDevice->SetVertexShader( ... );
    pd3dDevice->SetIndices( ... );

    pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 
                g_dwNumVertices, 0, g_dwNumIndices/3 );
    
    pd3dDevice->EndScene();
}

I think what we should be checking for is D3DSTREAMSOURCE_INDEXEDDATA.

@K0bin
Copy link
Collaborator Author

K0bin commented Jan 10, 2023

Needs more investigation

@K0bin K0bin closed this Jan 10, 2023
@K0bin K0bin reopened this Jan 13, 2023
@K0bin
Copy link
Collaborator Author

K0bin commented Jan 13, 2023

I tested this on Windows and instancing indeed only happens with indexed draws.

cc @Joshua-Ashton

@K0bin K0bin force-pushed the 40-billion-points branch from 148dedd to 8f708c0 Compare January 13, 2023 00:50
@K0bin K0bin force-pushed the 40-billion-points branch from 8f708c0 to b7cdfe0 Compare January 13, 2023 21:49
@misyltoad misyltoad merged commit 97a91c8 into doitsujin:master Jan 13, 2023
@K0bin K0bin deleted the 40-billion-points branch January 13, 2023 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Saints Rows 3 at 1 fps!!
3 participants