# How do I toggle color writes in SlimDX 11/Direct3D 11 with FeatureLevel 10?

I'm having trouble setting the RenderTargetWriteMask field of a RenderTargetDescription struct. I simply want to toggle color writes but every time I try to enable the color writes are changed back to zero. Here is the code:

BlendState bs = null, bs1 = null;

try
{
RenderTargetBlendDescription rtBlendDesc = new RenderTargetBlendDescription()
{
BlendEnable = false,
DestinationBlend = BlendOption.Zero,
DestinationBlendAlpha = BlendOption.Zero,
SourceBlend = BlendOption.One,
SourceBlendAlpha = BlendOption.One,
};

// default blend state
bs = BlendState.FromDescription(_device, new BlendStateDescription()
{
AlphaToCoverageEnable = false,
IndependentBlendEnable = false,

});
for (int i = 0; i < Constants.MaxSimultaneousRenderTargets; ++i)
bs.Description.RenderTargets[i] = rtBlendDesc;

// set default blend state
_device.ImmediateContext.OutputMerger.BlendState = bs;

// in debug you will see that the RenderTargetWriteMask field of each render target is set to 'None'...
}
catch (Exception e) { e.Handle(); F.DisposeResource(ref bs); F.DisposeResource(ref bs1); }


Update: I'm not able to set any of the values stored in RenderTargetBlendDescription.

• I'm confused; you know that ColorWriteMaskFlags.All means enable all writes, right? Also, are you saying that bs.Description.RenderTargets[i].RenderTargetWriteMask is 0 for all i after that for loop? Or after the assignment of bs to BlendState?
– user1430
Mar 16 '16 at 21:18
• @JoshPetrie ...i am trying to toggle color writes...my mistake for title...when instantiating a BlendState object the initial RenderTargetWriteMask values are ColorWriteFlags.Zero for each target. My first step is trying to enable all color writes...all values are 0 after for loop Mar 17 '16 at 3:29
• You probably know, but since IndependentBlendEnable = false, 1-7 will be ignored anyway. Just to cover all the bases, have you checked to make sure that Constants.MaxSimultaneousRenderTargets is not simply 0?
– Jon
Mar 17 '16 at 8:46
• @Jon yes the value stored in Constants.MaxSimultaneousRenderTargets is 8. I should also have mentioned that I am using FeatureLevel.Direct3D10 with the Direct3D11/SlimDX11 API. Mar 17 '16 at 9:58

States are immutable in D3D10. Once created you cannot modify the properties for the state; the Description is essentially read-only, and we allow the language semantics to enforce that for us (the description is a value type, which is why you cannot modify the properties of the description returned by the Description getter).

I had to enable color writes in the BlendStateDescription before instantiating the BlendState. Code:

                    RenderTargetBlendDescription rtBlendDesc = new RenderTargetBlendDescription()
{
BlendEnable = false,
DestinationBlend = BlendOption.Zero,
DestinationBlendAlpha = BlendOption.Zero,
SourceBlend = BlendOption.One,
SourceBlendAlpha = BlendOption.One,
};
BlendStateDescription bsDesc = new BlendStateDescription()
{
AlphaToCoverageEnable = false,
IndependentBlendEnable = false
};

bsDesc.RenderTargets[0] = rtBlendDesc;

// default blend state
bs = BlendState.FromDescription(_device, bsDesc);