1Name 2 3 ARB_shader_atomic_counter_ops 4 5Name Strings 6 7 GL_ARB_shader_atomic_counter_ops 8 9Contact 10 11 Daniel Rakos (daniel.rakos 'at' amd.com) 12 13Contributors 14 15 Daniel Rakos, AMD 16 Graham Sellers, AMD 17 Piers Daniell, NVIDIA 18 19Notice 20 21 Copyright (c) 2015 The Khronos Group Inc. Copyright terms at 22 http://www.khronos.org/registry/speccopyright.html 23 24Specification Update Policy 25 26 Khronos-approved extension specifications are updated in response to 27 issues and bugs prioritized by the Khronos OpenGL Working Group. For 28 extensions which have been promoted to a core Specification, fixes will 29 first appear in the latest version of that core Specification, and will 30 eventually be backported to the extension document. This policy is 31 described in more detail at 32 https://www.khronos.org/registry/OpenGL/docs/update_policy.php 33 34Status 35 36 Complete. Approved by the ARB on June 26, 2015. 37 Ratified by the Khronos Board of Promoters on August 7, 2015. 38 39Version 40 41 Last Modified Date: 06/11/2015 42 Author Revision: 6 43 44Number 45 46 ARB Extension #182 47 48Dependencies 49 50 This extension is written against version 4.50 of the OpenGL Shading 51 Language Specification. 52 53 OpenGL 4.2 or ARB_shader_atomic_counters is required. 54 55 56Overview 57 58 The ARB_shader_atomic_counters extension introduced atomic counters, but 59 it limits list of potential operations that can be performed on them to 60 increment, decrement, and query. This extension extends the list of GLSL 61 built-in functions that can operate on atomic counters. The list of new 62 operations include: 63 64 * Addition and subtraction 65 * Minimum and maximum 66 * Bitwise operators (AND, OR, XOR, etc.) 67 * Exchange, and compare and exchange operators 68 69 70New Procedures and Functions 71 72 None. 73 74 75New Tokens 76 77 None. 78 79 80Modifications to the OpenGL Shading Language Specification, Version 4.50 81 82Additions to Chapter 8 of the OpenGL Shading Language Specification 83(Built-in Functions) 84 85 Modify Section 8.10, Atomic-Counter Functions 86 87 (remove second paragraph on p. 173 starting with "The value returned...") 88 89 (modify third paragraph on p. 173) 90 91 The underlying counter is a 32-bit unsigned integer. The result of 92 operations will wrap to [0, 2^32-1]. 93 94 (add to the table of atomic counter built-in functions on p. 173) 95 96 +-------------------------------------+------------------------------------------------------------------+ 97 | Syntax | Description | 98 +-------------------------------------+------------------------------------------------------------------+ 99 | uint atomicCounterAddARB( | Atomically | 100 | atomic_uint c, | 1. adds the value of <data> to the counter for c, and | 101 | uint data) | 2. returns its value prior to the operation. | 102 | | These two steps are done atomically with respect to the atomic | 103 | | counter functions in this table. | 104 +-------------------------------------+------------------------------------------------------------------+ 105 | uint atomicCounterSubtractARB( | Atomically | 106 | atomic_uint c, | 1. subtracts the value of <data> from the counter for c, and | 107 | uint data) | 2. returns its value prior to the operation. | 108 | | These two steps are done atomically with respect to the atomic | 109 | | counter functions in this table. | 110 +-------------------------------------+------------------------------------------------------------------+ 111 | uint atomicCounterMinARB( | Atomically | 112 | atomic_uint c, | 1. sets the counter for c to the minimum of the value of the | 113 | uint data) | counter and the value of <data>, and | 114 | | 2. returns the value prior to the operation. | 115 | | These two steps are done atomically with respect to the atomic | 116 | | counter functions in this table. | 117 +-------------------------------------+------------------------------------------------------------------+ 118 | uint atomicCounterMaxARB( | Atomically | 119 | atomic_uint c, | 1. sets the counter for c to the maximum of the value of the | 120 | uint data) | counter and the value of <data>, and | 121 | | 2. returns the value prior to the operation. | 122 | | These two steps are done atomically with respect to the atomic | 123 | | counter functions in this table. | 124 +-------------------------------------+------------------------------------------------------------------+ 125 | uint atomicCounterAndARB( | Atomically | 126 | atomic_uint c, | 1. sets the counter for c to the result of the bitwise AND of | 127 | uint data) | the value of the counter and the value of <data>, and | 128 | | 2. returns the value prior to the operation. | 129 | | These two steps are done atomically with respect to the atomic | 130 | | counter functions in this table. | 131 +-------------------------------------+------------------------------------------------------------------+ 132 | uint atomicCounterOrARB( | Atomically | 133 | atomic_uint c, | 1. sets the counter for c to the result of the bitwise OR of | 134 | uint data) | the value of the counter and the value of <data>, and | 135 | | 2. returns the value prior to the operation. | 136 | | These two steps are done atomically with respect to the atomic | 137 | | counter functions in this table. | 138 +-------------------------------------+------------------------------------------------------------------+ 139 | uint atomicCounterXorARB( | Atomically | 140 | atomic_uint c, | 1. sets the counter for c to the result of the bitwise XOR of | 141 | uint data) | the value of the counter and the value of <data>, and | 142 | | 2. returns the value prior to the operation. | 143 | | These two steps are done atomically with respect to the atomic | 144 | | counter functions in this table. | 145 +-------------------------------------+------------------------------------------------------------------+ 146 | uint atomicCounterExchangeARB( | Atomically | 147 | atomic_uint c, | 1. sets the counter value for c to the value of <data>, and | 148 | uint data) | 2. returns its value prior to the operation. | 149 | | These two steps are done atomically with respect to the atomic | 150 | | counter functions in this table. | 151 +-------------------------------------+------------------------------------------------------------------+ 152 | uint atomicCounterCompSwapARB( | Atomically | 153 | atomic_uint c, | 1. compares the value of <compare> and the counter value for c,| 154 | uint compare, | 2. if the values are equal, sets the counter value for c to | 155 | uint data) | the value of <data>, and | 156 | | 3. returns its value prior to the operation. | 157 | | These three steps are done atomically with respect to the atomic | 158 | | counter functions in this table. | 159 +-------------------------------------+------------------------------------------------------------------+ 160 161 162Errors 163 164 None. 165 166New State 167 168 None. 169 170New Implementation Dependent State 171 172 None. 173 174Usage Examples 175 176 Example 1: Appending variable length records to a buffer using a compute shader 177 178 // Here we use atomicCounterAdd to ensure that we "allocate" a contiguous list of 179 // elements in the buffer. 180 // Using atomicCounterIncrement cannot mimic this behavior as there is no guarantee 181 // that subsequent calls to atomicCounterIncrement return subsequent counter values. 182 183 layout(binding=0, offset=0) uniform atomic_uint vlrCounter; 184 layout(binding=0, r32ui) uniform imageBuffer vlrBuffer; 185 186 void main() 187 { 188 uint data[...] = ...; 189 uint recordLength = ...; 190 191 uint recordStart = atomicCounterAddARB(vlrCounter, recordLength); 192 193 for (uint i = 0; i < recordLength; ++i) 194 { 195 imageStore(vlrBuffer, recordStart + i, data[i]); 196 } 197 } 198 199Issues 200 201 (1) What additional operations should this extension introduce? 202 203 RESOLVED: The following new operations are generally supported by 204 target hardware: 205 * Addition and subtraction 206 * Minimum and maximum 207 * Bitwise operators (AND, OR, XOR, etc.) 208 * Exchange, and compare and exchange operators 209 210 (2) How the functionality in this extension is different than that 211 introduced by AMD_shader_atomic_counter_ops? 212 213 RESOLVED: In addition to the operations introduced by this extension 214 AMD_shader_atomic_counter_ops also adds support for wrapped increment, 215 wrapped decrement, reverse subtract, and a special masked-or operation. 216 Also, the semantics of the subtract operation is different between this 217 and the AMD extension. 218 219 (3) Should the new decrement with wrap and subtract operators match the 220 behavior of the existing decrement operator and return the post- 221 operation value of the counter? 222 223 RESOLVED: Yes, for consistency with the existing functionality. 224 225 (4) Most of the new operations introduced by this extension are already 226 supported on load/store images and shader storage buffers. What benefit 227 the application developer could get from using this extension? 228 229 DISCUSSION: Atomic counter operations can have different performance 230 characteristics than load/store images or shader storage buffers. It is 231 expected that implementations can do atomic counter operations faster. 232 233 RESOLVED: It enables existing operations for atomic counters and also 234 adds completely new operations that are not available for load/store 235 images and shader storage buffers. 236 237 (5) Should the new subtract operator match the behavior of the existing 238 decrement operator and return the post-operation value of the counter? 239 240 RESOLVED: No, it returns the value of the counter prior to the operation, 241 just like other atomic counter operations except decrement. 242 243Revision History 244 245 Rev. Date Author Changes 246 ---- -------- -------- ------------------------------------------------- 247 6 06/11/15 drakos Updated resolution of issue (2) and (5) based on 248 the previous change. 249 5 05/28/15 pdaniell Modify the return semantics of 250 atomicCounterSubtractARB to return the result 251 prior to the operation. 252 4 05/08/15 drakos Added ARB suffixes to the built-in functions. 253 3 03/18/15 drakos Removed reverse subtract. 254 2 03/17/15 drakos Removed wrapped increment, wrapped decrement, and 255 masked-or operations. Also resolved outstanding 256 issues. 257 1 02/03/15 drakos Draft based on AMD_shader_atomic_counter_ops. 258 Rebased language for GLSL 4.50. 259