Sign in to follow this  

Per Material constant buffer?

This topic is 2355 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

I'm converting a DX9 engine over to DX10/11 and trying to group things into constant buffers by frequency of update. I am trying to organize the renderer to minimize state changes which is straight forward when implementing constant buffers if I understand it right.

- Per Scene: Global light source, time, environment settings
- Per Pass: Blend mode, Near/Far planes, FOV, Projection matrix
- Per Mesh: ModelToWorld matrix,

But when implementing materials I'm not sure where to put the material parameters. Each mesh can have a different material so each mesh could have different constants. For example one mesh might have diffuse only while another might have diffuse, normal and specular constants. Given that each mesh might have a different number of constants I wouldn't think these belong in the PerMesh category. But I also don't want to put them all in some global constant buffer (i.e. $global) because as the size of the buffer increases so does the chance that it will need to be updated/reuploaded if one of the parameter changes. So how do you guys recommend handling this case?

Share this post


Link to post
Share on other sites
You are not going to get a speed hit if you use a cbuffer with 3 components over one with 2 components. So, keep it simple. Now, I do not know all of your different possible settings for models, but if you need a cbuffer with 4 components to satisfy all of your model rendering needs, then go with that. Instead of creating 5 different cbuffers with different variable names, then you have to create different techniques. This is a real headache, and you likely will not get any speed increase at all.

So, go with a 3 or 4 component cbuffer for your model rendering needs and you will be happy you did.

Share this post


Link to post
Share on other sites
Well, I would go that route, but it's a bit more complicated. This is a data-driven renderer where shader constants and materials are created by artists. So materials could range from 1 constant (say, emission color) to 30 constants (a very complicated shader). What's worse is that I'm not guaranteed that the 1 constant (emission) will be contained in the larger material so i'd have to concatenate if it's not found. So in the end I could end up having 50 or so parameters in the "material" constant buffer. This means for each material group I'd need to reupload all 50 constants even when only one of them has changed. This is why I'm thinking it might be better to actually create, store and manage the material constants in their own constant buffer and simply bind the material constant buffers as the renderer needs them (sort renderables by material as to minimize the number of constant buffer uploads). This may be overkill though, which is why I'm asking the experts here on the forum.

Share this post


Link to post
Share on other sites
If you have up to 50 constants in one buffer (that aren't an array like skinning matrices) then you should consider grouping the constants into related groups and then using the groups as your boundaries for your constant buffers. You want to avoid having to re-upload large amounts of data for every rendering, so simply group the constants that are used together and update only the stuff you need to for each rendering call. Don't get hung up on the per-mesh, per-material, etc... naming. You want per-related group of constants!

Share this post


Link to post
Share on other sites

This topic is 2355 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.

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