ATOMICCOMPSWAP(3G)
π
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 withmem. 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
memis equal tocompareβ the content ofdatagets written intomem. slay, we swapped π - otherwise β the content of
memis 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 π
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
- ubuntu jammy: opengl-4-man-doc 1.0~svn33624-1 Β· SGI-Free-Software-B-License-2
- ubuntu noble: opengl-4-man-doc 1.0~svn33624-1 Β· SGI-Free-Software-B-License-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms