2
\$\begingroup\$

If I want to set explicitly every parameter of the structure I just do so and that's it. But what if I want to set only some of them and leave the rest with default values? I've seen in some tutorial zeroing the structure:

ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));

but the default values (http://msdn.microsoft.com/en-us/library/windows/desktop/ff476207%28v=vs.85%29.aspx) aren't always 0. Is this incorrect? Should I always specify all values explicitly (especially LOD values seem tricky)?

I'm asking about D3D11_SAMPLER_DESC but maybe the same rules apply to other DX structures?

\$\endgroup\$
3
\$\begingroup\$

but the default values aren't always 0. Is this incorrect?

If it was just the default values being different from 0, then zeroing the desc struct wouldn't necessarily be incorrect, it just wouldn't give you a default sampler state.

However for some of the members (such as D3D11_TEXTURE_ADDRESS_MODE AddressU, AddressV, AddressW) 0 is not even a valid value and creating the sampler state will fail, so yes it's incorrect here.

To avoid having to specify the defaults every time, you could write a wrapper, an init or "factory" function, or store a const instance with the default values set somewhere.
D3D already comes with some Helper Structures which wrap many of the DESC structs and can intialize them with the proper default values for you (as long as you don't define D3D11_NO_HELPERS).

\$\endgroup\$
  • \$\begingroup\$ So regardless of the actual implementation you say that I have to explicitly zero or set all params? \$\endgroup\$ – NPS Dec 27 '13 at 0:10
  • \$\begingroup\$ Yes. I looked, but there seem to be no clever ways to create a sampler state without a desc struct, or to obtain the desc from the default sampler state used when setting a NULL sampler state to a slot. \$\endgroup\$ – user13213 Dec 27 '13 at 4:42
  • 2
    \$\begingroup\$ @melak47 Many of the *_DESC structs have corresponding C*_DESC helper classes with meaningful default constructors. In this case, look at CD3D11_SAMPLER_DESC(CD3D11_DEFAULT). \$\endgroup\$ – MooseBoys Jan 9 '14 at 21:10
  • \$\begingroup\$ @MooseBoys you're right, I completely forgot about these! \$\endgroup\$ – user13213 Jan 9 '14 at 21:18
1
\$\begingroup\$

Zeroing the structure is incorrect. Some of the enum types do not have zero as a valid value, but that's likely to be fairly harmless as the D3D runtime will probably just substitute default values for zeroes (possibly with a warning emitted to the debug layer; I haven't checked). However, where zeroing will really foul you up is with the MaxLOD member, where setting it to zero effectively disables mipmaps on that texture. That's definitely something you'd need to watch out for.

Personally, I would just always explicitly initialize all members of the sampler struct.

\$\endgroup\$
  • 1
    \$\begingroup\$ I checked, creating a sampler state with a zeroed desc will fail with CREATESAMPLERSTATE_INVALIDADDRESSU, V, W. \$\endgroup\$ – user13213 Dec 27 '13 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.