• Advertisement
Sign in to follow this  

COM Interface best practices

Recommended Posts

The COM interfaces of DXGI and D3D11, that I use, have multiple extensions adding extra functionality to the previous version of those interfaces:

  • IDXGIAdapter (up to suffix 4)
  • IDXGIOutput (up to suffix 6)
  • IDXGIFactory (up to suffix 5)
  • ID3D11Device (up to suffix 5)
  • ID3D11DeviceContext (up to suffix 4)

Currently, I used the newest of all these interfaces in my code. Even though I do not use all the new features, I thought it would be the most beneficial in the long run to go for the most expressive interface. Due to some 3th party software compatibility, I need to use older versions, which luckily does not impose a problem with regard to the functionality I require.

I wonder what are best practices with regard to deciding which interfaces to use as the type of member variables for manager objects owning the COM objects:

  • In the presence of no compatibility problems (not with 3th party software, not with the user base), should one go for the most expressive interfaces everywhere?
  • Should one go for the minimal required interfaces everywhere? (e.g. if only functionality up to IDXGIOutput2 is used, IDXGIOutput2 will be used everywhere)
  • Should one go for the minimal interfaces as member variables, and retrieve the more expressive interfaces on the fly when needed? (e.g. IDXGIOutput will be stored, but IDXGIOutput2 will be retrieved if needed.)

 

Share this post


Link to post
Share on other sites
Advertisement

I think the common practice was bullet point #3 wherein you use the least common denominator and then fetch the latest interface if required.

Share this post


Link to post
Share on other sites

There is actually a rule of thumb here which is fairly easy.  If you intend to supply fallback functionality for when you can not get say IDXGIAdapter4, then following point three is the best way to go.  If, on the other hand, you absolutely require a certain level of the interface to exist, say you absolutely must have IDXGIAdapter3 or higher, then keep only IDXGIAdapter3 pointers.  Basically there is no reason to have multiple pointers or continually query things unless you can actually handle failures and have supplied alternative paths through the code for those cases.

A case in point, I'm doing work with DX12 and as such there are guaranteed minimum versions of DXGI which must exist for DX12 devices to even be created.  I have no fallbacks since I can't get DX12 functional without the minimums so any failure is a terminal error.  On the other hand, I do query for 12.1 features in a couple places just because they make things a bit easier, but I have fallbacks to 12.0 implementations, in those specific cases where it matters, yes I query as needed.

Hope this makes sense and is helpful.

Share this post


Link to post
Share on other sites
17 hours ago, All8Up said:

Hope this makes sense and is helpful.

Thank you, much appreciated. Your clarification is actually a refinement of my 3th dot. Instead of always using the base interface (without a suffix), you use the one corresponding to the minimal required specs.

I maybe even consider adding a typedef, to easily increase the interface in case the required specs change.

Edited by matt77hias

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement