1
\$\begingroup\$

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()
    {
        RenderTargetWriteMask = ColorWriteMaskFlags.All,
        BlendEnable = false,
        BlendOperation = BlendOperation.Add,
        BlendOperationAlpha = BlendOperation.Add,
        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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Josh Mar 16 '16 at 21:18
  • \$\begingroup\$ @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 \$\endgroup\$ – P. Avery Mar 17 '16 at 3:29
  • \$\begingroup\$ 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? \$\endgroup\$ – Jon Mar 17 '16 at 8:46
  • \$\begingroup\$ @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. \$\endgroup\$ – P. Avery Mar 17 '16 at 9:58
1
\$\begingroup\$

I found the answer in this thread which says:

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()
                    {
                        RenderTargetWriteMask = ColorWriteMaskFlags.All,
                        BlendEnable = false,
                        BlendOperation = BlendOperation.Add,
                        BlendOperationAlpha = BlendOperation.Add,
                        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);
\$\endgroup\$

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.