ATOMICCOMPSWAP(3G)

πŸ“¦
version 1.0~svn33624 Β· 4 versions
, in ubuntu jammy, ubuntu noble

πŸ’… ATOMICCOMPSWAP β€” the 3G manual page, bestie edition ✨

okay so real talk, this is atomicCompSwap from the 3G manual section (the header literally has a [FIXME: manual] in it, iconic of the Khronos group honestly 🫢). It's giving "tiny function, huge responsibility". Let's go.


🌸 NAME 🌸

atomicCompSwap β€” perform an atomic compare-exchange operation to a variable


✨ DECLARATION ✨

no cap, there are literally two of them and you have to pick the one that matches your type:

int atomicCompSwap(inout int mem, uint compare, uint data);

uint atomicCompSwap(inout uint mem, uint compare, uint data);

πŸŽ€ PARAMETERS πŸŽ€

  • mem β€” the variable you're using as the target of the operation. like, the thing getting poked. πŸ‘ˆ
  • data β€” the data that's gonna be compared and potentially exchanged with mem. it's the maybe-girlfriend of this whole situation.

πŸ’– DESCRIPTION πŸ’–

okay so here's the tea β˜•. atomicCompSwap performs an atomic comparison of compare with the contents of mem. and then:

  • if the content of mem is equal to compare β†’ the content of data gets written into mem. slay, we swapped πŸ’…
  • otherwise β†’ the content of mem is unmodified. it said "not today". πŸ™ˆ

but here's the part that actually matters: the function returns the original content of mem regardless of the outcome of the comparison. so you always get the old value back, whether the swap happened or not. that's the whole point, iykyk.

and the safety guarantee, which is the actual main character energy here:

The contents of the memory being updated by the atomic operation are guaranteed not to be modified by any other assignment or atomic memory function in any shader invocation between the time the original value is read and the time the new value is written. πŸ”₯

like, nobody else can sneak in and touch it mid-operation. periodt.

now, the vibe check that trips people up: atomic memory functions are supported only for a limited set of variables. if you pass something to the mem argument of an atomic memory function that does not correspond to a buffer or shared variable, a shader will fail to compile. not a warning, bestie. a fail. πŸ’€

the good news? it is acceptable to pass an element of an array or a single component of a vector to the mem argument of an atomic memory function β€” as long as the underlying array or vector is a buffer or shared variable. so like, you can absolutely reach into an array or pick one component of a vector, you just can't do it to some random local. got it? 🫢


🌈 VERSION SUPPORT 🌈

here's the rundown of which OpenGL Shading Language versions said yes to atomicCompSwap. she was late to the party but she ATE when she arrived πŸ‘‘

  • 1.10 β€” -
  • 1.20 β€” -
  • 1.30 β€” -
  • 1.40 β€” -
  • 1.50 β€” -
  • 3.30 β€” -
  • 4.00 β€” -
  • 4.10 β€” -
  • 4.20 β€” -
  • 4.30 β€” βœ”
  • 4.40 β€” βœ”
  • 4.50 β€” βœ”

so yeah, 4.30 was her debut. everything before that? not invited. ✨


πŸ¦‹ SEE ALSO πŸ¦‹

the whole atomic friend group, go say hi:

  • atomicAdd()
  • atomicAnd()
  • atomicOr()
  • atomicXor()
  • atomicMin()
  • atomicMax()
  • atomicExchange()

Copyright Β© 2011-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

Copyright Β© 2012-2014 Khronos Group


and that's the whole page, bestie πŸ’œ sourced from [FIXME: source] and dated 01/03/2018, filed under ATOMICCOMPSWAP(3G). go forth and swap atomically. πŸ§šβ€β™€οΈ

πŸ“¦ where this page comes from