How does "binding" operation work internally and how expensive is it?

Started by
1 comment, last by SoldierOfLight 5 years, 10 months ago

Hey,

I can't find this information anywhere on the web and I'm wondering about specific optimization... Let's say I have hundreds of 3D textures which I need to process separately in compute shader. Each invocation needs different data in constant buffer BUT many of the 3d textures don't need to update their CB contents every frame. Would it be better to create just one CB resource, bind just once at startup and in loop map the data for each consecutive shader invocation or would it be better to create like hundreds of separate CB resources, map them only when needed and just bind appropriate CB before each shader invocation? This depends on how exacly are those resources managed internally in DirectX and what does binding actually do... I would be very grateful if somebody shared their experience!

Advertisement

Depends on the sizes we're talking about here. Generally, using Map(WRITE_DISCARD) on a CB results in re-allocating the entire buffer. If you're talking about a few bytes per texture, then it's probably more efficient to cram them all together, as individual CBs would waste a lot of memory in padding. If you're talking about a few hundred bytes per texture, it probably makes more sense to only re-allocate/bind the one that's relevant at the moment.

The actual hardware implications of "binding" CBs and textures vary across different hardware, even on the same vendor, but generally binding a CB is very cheap, while binding textures is less cheap. If you're interested in not needing to change texture bindings as often, you can take a look at DX12 with its bindless (read: bind fewer times, not never) options. But I'd recommend profiling before deciding that changing your bindings is the bottleneck.

This topic is closed to new replies.

Advertisement