Logical Operation: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
m linking
Filled out the page; no longer a stub
Line 1: Line 1:
{{pipeline float}}
{{pipeline float}}
A '''Logical Operation''' is a per-sample operation applied between the fragment's color values and color values in the corresponding sample in the framebuffer. They are performed independently for the RGBA components of the colors, and they only work for framebuffer attachments that are integers (normalized or not). And even then, they do not work for [[Framebuffer#Colorspace|rendering to sRGB images]] if {{enum|GL_FRAMEBUFFER_SRGB}} is active.
A '''Logical Operation''' is a [[Per-Sample Processing]] operation applied between the [[Fragment]]'s color values and color values in the [[Framebuffer]] being rendered to that correspond to that particular fragment color. Logical operations are boolean operations performed on the bit pattern that represents the colors.


Enabling logical operations automatically turns off [[Blending]].
__TOC__


{{stub}}
== Activation ==
 
To activate logical operations, {{enum|GL_COLOR_LOGIC_OP}} must be enabled. Whenever logical operations are enabled, all [[Blending]] operations are disabled.
 
Because logical operations are bitwise boolean operations, there are some times when such operations are not appropriate. These are defined by attributes of the image attached to the framebuffer that [[Framebuffer#Draw color buffers|corresponds to that fragment color]]. In these cases, logical operations do not take place for those specific colors; other colors that do not fit this will use the logical operation if it is enabled. The cases are:
 
* If the framebuffer image does not use an integer [[Image Format]] ([[Normalized Integer|normalized]] or not).
 
* If [[Framebuffer#Colorspace|sRGB rendering is enabled]], and the framebuffer image's image format is [[Image Format#sRGB colorspace|in the sRGB colorspace]].
 
== Operations ==
 
There are a number of different logical operations available. Note that, unlike [[Blending]], OpenGL logical operations cannot perform separate operations for different fragment colors. All fragment colors (except for those which can't use logic ops, as above) will use the same operation.
 
The particular operation to use is set by the following function:
 
{{funcdef|void {{apifunc|glLogicOp}}(GLenum {{param|opcode}});}}
 
The {{param|opcode}} defines which operation to use (the default value is {{enum||GL_COPY}}. The available logic ops, and their result, is listed in the following table. Logic ops are performed individually per-component.
 
The component value for the fragment color is called '''S'''; the component value from the framebuffer image is called '''D'''. "&" is a [https://en.wikipedia.org/wiki/Bitwise_operation#AND bitwise and], "|" is a [https://en.wikipedia.org/wiki/Bitwise_operation#OR bitwise or], "^" is the [https://en.wikipedia.org/wiki/Bitwise_operation#XOR bitwise xor], and ~ is the [https://en.wikipedia.org/wiki/Bitwise_operation#NOT bitwise negation].
 
{|
|-
! '''Opcode'''
! '''Resulting Operation'''
|-
| {{enum|GL_CLEAR}}
| 0
|-
| {{enum|GL_SET}}
| 1
|-
| {{enum|GL_COPY}}
| '''S'''
|-
| {{enum|GL_COPY_INVERTED}}
| ~'''S'''
|-
| {{enum|GL_NOOP}}
| '''D'''
|-
| {{enum|GL_INVERT}}
| ~'''D'''
|-
| {{enum|GL_AND}}
| '''S''' & '''D'''
|-
| {{enum|GL_NAND}}
| ~('''S''' & '''D''')
|-
| {{enum|GL_OR}}
| '''S''' | '''D'''
|-
| {{enum|GL_NOR}}
| ~('''S''' | '''D''')
|-
| {{enum|GL_XOR}}
| '''S''' ^ '''D'''
|-
| {{enum|GL_EQUIV}}
| ~('''S''' ^ '''D''')
|-
| {{enum|GL_AND_REVERSE}}
| '''S''' & ~'''D'''
|-
| {{enum|GL_AND_INVERTED}}
| ~'''S''' & '''D'''
|-
| {{enum|GL_OR_REVERSE}}
| '''S''' | ~'''D'''
|-
| {{enum|GL_OR_INVERTED}}
| ~'''S''' | '''D'''
|}


[[Category:Sample Writing]]
[[Category:Sample Writing]]

Revision as of 23:56, 30 January 2015

A Logical Operation is a Per-Sample Processing operation applied between the Fragment's color values and color values in the Framebuffer being rendered to that correspond to that particular fragment color. Logical operations are boolean operations performed on the bit pattern that represents the colors.

Activation

To activate logical operations, GL_COLOR_LOGIC_OP must be enabled. Whenever logical operations are enabled, all Blending operations are disabled.

Because logical operations are bitwise boolean operations, there are some times when such operations are not appropriate. These are defined by attributes of the image attached to the framebuffer that corresponds to that fragment color. In these cases, logical operations do not take place for those specific colors; other colors that do not fit this will use the logical operation if it is enabled. The cases are:

Operations

There are a number of different logical operations available. Note that, unlike Blending, OpenGL logical operations cannot perform separate operations for different fragment colors. All fragment colors (except for those which can't use logic ops, as above) will use the same operation.

The particular operation to use is set by the following function:

void glLogicOp(GLenum opcode​);

The opcode​ defines which operation to use (the default value is . The available logic ops, and their result, is listed in the following table. Logic ops are performed individually per-component.

The component value for the fragment color is called S; the component value from the framebuffer image is called D. "&" is a bitwise and, "|" is a bitwise or, "^" is the bitwise xor, and ~ is the bitwise negation.

Opcode Resulting Operation
GL_CLEAR 0
GL_SET 1
GL_COPY S
GL_COPY_INVERTED ~S
GL_NOOP D
GL_INVERT ~D
GL_AND S & D
GL_NAND ~(S & D)
GL_OR D
GL_NOR D)
GL_XOR S ^ D
GL_EQUIV ~(S ^ D)
GL_AND_REVERSE S & ~D
GL_AND_INVERTED ~S & D
GL_OR_REVERSE ~D
GL_OR_INVERTED D