10. Logical Operations

Before a fragment is written to the framebuffer, a logical operation is applied which uses the incoming fragment values as source (s) and/or those currently stored in the color buffer as destination (d). After the selected operation is completed, destination is overwritten. Logical operations are performed independently for each Red, Green, Blue and Alpha value and if the framebuffer has multiple color attachments, the logical operation is computed and applied separately for each color buffer.

If Logic Op is enabled, OpenGL behaves as if Blending is disabled regardless whether it was previously enabled.

In order to apply the Logicial Operation, use EnableCap.ColorLogicOp

GL.Enable( EnableCap.ColorLogicOp );
GL.Disable( EnableCap.ColorLogicOp ); // default

Note: If you use EnableCap.LogicOp or EnableCap.IndexLogicOp, only indexed color buffers (8 Bit) are affected.

To select the logical operation to be performed, use GL.LogicOp( op ); where op is by default LogicOp.Copy.

  • LogicOp.Clear: 0
  • LogicOp.And: s & d
  • LogicOp.AndReverse: s & !d
  • LogicOp.Copy: s
  • LogicOp.AndInverted: !s & d
  • LogicOp.Noop: d
  • LogicOp.Xor: s XOR d
  • LogicOp.Or: s | d
  • LogicOp.Nor: !(s | d)
  • LogicOp.Equiv: !(s XOR d)
  • LogicOp.Invert: !d
  • LogicOp.OrReverse: s | !d
  • LogicOp.CopyInverted: !s
  • LogicOp.OrInverted: !s | d
  • LogicOp.Nand: !(s & d)
  • LogicOp.Set: all 1's

State Queries
The state of LogicOp can be queried with Result = GL.IsEnabled( EnableCap.LogicOp );

Which operation has been set through GL.LogicOp() can be queried with GL.GetInteger( GetPName.LogicOpMode, ... )