blend.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // Copyright 2022 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebiten
  15. import (
  16. "fmt"
  17. "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
  18. )
  19. // Blend is a blending way of the source color and the destination color.
  20. //
  21. // The final color is calculated like this:
  22. //
  23. // c_src: source RGB values
  24. // c_dst: destination RGB values
  25. // c_out: result RGB values
  26. // α_src: source alpha values
  27. // α_dst: destination alpha values
  28. // α_out: result alpha values
  29. //
  30. // c_out = BlendOperationRGB((BlendFactorSourceRGB) × c_src, (BlendFactorDestinationRGB) × c_dst)
  31. // α_out = BlendOperationAlpha((BlendFactorSourceAlpha) × α_src, (BlendFactorDestinationAlpha) × α_dst)
  32. //
  33. // A blend factor is a factor for source and color destination color values.
  34. // The default is source-over (regular alpha blending).
  35. //
  36. // A blend operation is a binary operator of a source color and a destination color.
  37. // The default is adding.
  38. type Blend struct {
  39. // BlendFactorSourceRGB is a factor for source RGB values.
  40. BlendFactorSourceRGB BlendFactor
  41. // BlendFactorSourceAlpha is a factor for source alpha values.
  42. BlendFactorSourceAlpha BlendFactor
  43. // BlendFactorDestinationRGB is a factor for destination RGB values.
  44. BlendFactorDestinationRGB BlendFactor
  45. // BlendFactorDestinationAlpha is a factor for destination alpha values.
  46. BlendFactorDestinationAlpha BlendFactor
  47. // BlendOperationRGB is an operation for source and destination RGB values.
  48. BlendOperationRGB BlendOperation
  49. // BlendOperationAlpha is an operation for source and destination alpha values.
  50. BlendOperationAlpha BlendOperation
  51. }
  52. var (
  53. defaultBlendInternalBlend = graphicsdriver.Blend{
  54. BlendFactorSourceRGB: BlendFactorDefault.internalBlendFactor(true),
  55. BlendFactorSourceAlpha: BlendFactorDefault.internalBlendFactor(true),
  56. BlendFactorDestinationRGB: BlendFactorDefault.internalBlendFactor(false),
  57. BlendFactorDestinationAlpha: BlendFactorDefault.internalBlendFactor(false),
  58. BlendOperationRGB: BlendOperationAdd.internalBlendOperation(),
  59. BlendOperationAlpha: BlendOperationAdd.internalBlendOperation(),
  60. }
  61. )
  62. func (b Blend) internalBlend() graphicsdriver.Blend {
  63. // A shortcut for the most common blend.
  64. if b == (Blend{}) {
  65. return defaultBlendInternalBlend
  66. }
  67. return graphicsdriver.Blend{
  68. BlendFactorSourceRGB: b.BlendFactorSourceRGB.internalBlendFactor(true),
  69. BlendFactorSourceAlpha: b.BlendFactorSourceAlpha.internalBlendFactor(true),
  70. BlendFactorDestinationRGB: b.BlendFactorDestinationRGB.internalBlendFactor(false),
  71. BlendFactorDestinationAlpha: b.BlendFactorDestinationAlpha.internalBlendFactor(false),
  72. BlendOperationRGB: b.BlendOperationRGB.internalBlendOperation(),
  73. BlendOperationAlpha: b.BlendOperationAlpha.internalBlendOperation(),
  74. }
  75. }
  76. // BlendFactor is a factor for source and destination color values.
  77. type BlendFactor byte
  78. const (
  79. // BlendFactorDefault is the default factor value.
  80. // The actual value depends on which source or destination this value is used.
  81. BlendFactorDefault BlendFactor = iota
  82. // BlendFactorZero is a factor:
  83. //
  84. // 0
  85. BlendFactorZero
  86. // BlendFactorOne is a factor:
  87. //
  88. // 1
  89. BlendFactorOne
  90. // BlendFactorSourceColor is a factor:
  91. //
  92. // (source RGBA)
  93. BlendFactorSourceColor
  94. // BlendFactorOneMinusSourceColor is a factor:
  95. //
  96. // 1 - (source color)
  97. BlendFactorOneMinusSourceColor
  98. // BlendFactorSourceAlpha is a factor:
  99. //
  100. // (source alpha)
  101. BlendFactorSourceAlpha
  102. // BlendFactorOneMinusSourceAlpha is a factor:
  103. //
  104. // 1 - (source alpha)
  105. BlendFactorOneMinusSourceAlpha
  106. // BlendFactorDestinationColor is a factor:
  107. //
  108. // (destination RGBA)
  109. BlendFactorDestinationColor
  110. // BlendFactorOneMinusDestinationColor is a factor:
  111. //
  112. // 1 - (destination RGBA)
  113. BlendFactorOneMinusDestinationColor
  114. // BlendFactorDestinationAlpha is a factor:
  115. //
  116. // (destination alpha)
  117. BlendFactorDestinationAlpha
  118. // BlendFactorOneMinusDestinationAlpha is a factor:
  119. //
  120. // 1 - (destination alpha)
  121. BlendFactorOneMinusDestinationAlpha
  122. // TODO: Add BlendFactorSourceAlphaSaturated. This might not work well on some platforms like Steam SDK (#2382).
  123. )
  124. func (b BlendFactor) internalBlendFactor(source bool) graphicsdriver.BlendFactor {
  125. switch b {
  126. case BlendFactorDefault:
  127. // The default is the source-over composition (regular alpha blending).
  128. if source {
  129. return graphicsdriver.BlendFactorOne
  130. }
  131. return graphicsdriver.BlendFactorOneMinusSourceAlpha
  132. case BlendFactorZero:
  133. return graphicsdriver.BlendFactorZero
  134. case BlendFactorOne:
  135. return graphicsdriver.BlendFactorOne
  136. case BlendFactorSourceColor:
  137. return graphicsdriver.BlendFactorSourceColor
  138. case BlendFactorOneMinusSourceColor:
  139. return graphicsdriver.BlendFactorOneMinusSourceColor
  140. case BlendFactorSourceAlpha:
  141. return graphicsdriver.BlendFactorSourceAlpha
  142. case BlendFactorOneMinusSourceAlpha:
  143. return graphicsdriver.BlendFactorOneMinusSourceAlpha
  144. case BlendFactorDestinationColor:
  145. return graphicsdriver.BlendFactorDestinationColor
  146. case BlendFactorOneMinusDestinationColor:
  147. return graphicsdriver.BlendFactorOneMinusDestinationColor
  148. case BlendFactorDestinationAlpha:
  149. return graphicsdriver.BlendFactorDestinationAlpha
  150. case BlendFactorOneMinusDestinationAlpha:
  151. return graphicsdriver.BlendFactorOneMinusDestinationAlpha
  152. default:
  153. panic(fmt.Sprintf("ebiten: invalid blend factor: %d", b))
  154. }
  155. }
  156. // BlendOperation is an operation for source and destination color values.
  157. type BlendOperation byte
  158. const (
  159. // BlendOperationAdd represents adding the source and destination color.
  160. //
  161. // c_out = (BlendFactorSourceRGB) × c_src + (BlendFactorDestinationRGB) × c_dst
  162. // α_out = (BlendFactorSourceAlpha) × α_src + (BlendFactorDestinationAlpha) × α_dst
  163. BlendOperationAdd BlendOperation = iota
  164. // BlendOperationSubtract represents subtracting the source and destination color.
  165. //
  166. // c_out = (BlendFactorSourceRGB) × c_src - (BlendFactorDestinationRGB) × c_dst
  167. // α_out = (BlendFactorSourceAlpha) × α_src - (BlendFactorDestinationAlpha) × α_dst
  168. BlendOperationSubtract
  169. // BlendOperationReverseSubtract represents subtracting the source and destination color in a reversed order.
  170. //
  171. // c_out = (BlendFactorDestinationRGB) × c_dst - (BlendFactorSourceRGB) × c_src
  172. // α_out = (BlendFactorDestinationAlpha) × α_dst - (BlendFactorSourceAlpha) × α_src
  173. BlendOperationReverseSubtract
  174. // BlendOperationMin represents a minimum function for the source and destination color.
  175. // If BlendOperationMin is specified, blend factors are not used.
  176. //
  177. // c_out = min(c_dst, c_src)
  178. // α_out = min(α_dst, α_src)
  179. BlendOperationMin
  180. // BlendOperationMax represents a maximum function for the source and destination color.
  181. // If BlendOperationMax is specified, blend factors are not used.
  182. //
  183. // c_out = max(c_dst, c_src)
  184. // α_out = max(α_dst, α_src)
  185. BlendOperationMax
  186. )
  187. func (b BlendOperation) internalBlendOperation() graphicsdriver.BlendOperation {
  188. switch b {
  189. case BlendOperationAdd:
  190. return graphicsdriver.BlendOperationAdd
  191. case BlendOperationSubtract:
  192. return graphicsdriver.BlendOperationSubtract
  193. case BlendOperationReverseSubtract:
  194. return graphicsdriver.BlendOperationReverseSubtract
  195. case BlendOperationMin:
  196. return graphicsdriver.BlendOperationMin
  197. case BlendOperationMax:
  198. return graphicsdriver.BlendOperationMax
  199. default:
  200. panic(fmt.Sprintf("ebiten: invalid blend operation: %d", b))
  201. }
  202. }
  203. // This name convention follows CSS compositing: https://drafts.fxtf.org/compositing-2/.
  204. //
  205. // In the comments,
  206. // c_src, c_dst and c_out represent alpha-premultiplied RGB values of source, destination and output respectively. α_src and α_dst represent alpha values of source and destination respectively.
  207. var (
  208. // BlendSourceOver is a preset Blend for the regular alpha blending.
  209. //
  210. // c_out = c_src + c_dst × (1 - α_src)
  211. // α_out = α_src + α_dst × (1 - α_src)
  212. BlendSourceOver = Blend{
  213. BlendFactorSourceRGB: BlendFactorOne,
  214. BlendFactorSourceAlpha: BlendFactorOne,
  215. BlendFactorDestinationRGB: BlendFactorOneMinusSourceAlpha,
  216. BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
  217. BlendOperationRGB: BlendOperationAdd,
  218. BlendOperationAlpha: BlendOperationAdd,
  219. }
  220. // BlendClear is a preset Blend for Porter Duff's 'clear'.
  221. //
  222. // c_out = 0
  223. // α_out = 0
  224. BlendClear = Blend{
  225. BlendFactorSourceRGB: BlendFactorZero,
  226. BlendFactorSourceAlpha: BlendFactorZero,
  227. BlendFactorDestinationRGB: BlendFactorZero,
  228. BlendFactorDestinationAlpha: BlendFactorZero,
  229. BlendOperationRGB: BlendOperationAdd,
  230. BlendOperationAlpha: BlendOperationAdd,
  231. }
  232. // BlendCopy is a preset Blend for Porter Duff's 'copy'.
  233. //
  234. // c_out = c_src
  235. // α_out = α_src
  236. BlendCopy = Blend{
  237. BlendFactorSourceRGB: BlendFactorOne,
  238. BlendFactorSourceAlpha: BlendFactorOne,
  239. BlendFactorDestinationRGB: BlendFactorZero,
  240. BlendFactorDestinationAlpha: BlendFactorZero,
  241. BlendOperationRGB: BlendOperationAdd,
  242. BlendOperationAlpha: BlendOperationAdd,
  243. }
  244. // BlendDestination is a preset Blend for Porter Duff's 'destination'.
  245. //
  246. // c_out = c_dst
  247. // α_out = α_dst
  248. BlendDestination = Blend{
  249. BlendFactorSourceRGB: BlendFactorZero,
  250. BlendFactorSourceAlpha: BlendFactorZero,
  251. BlendFactorDestinationRGB: BlendFactorOne,
  252. BlendFactorDestinationAlpha: BlendFactorOne,
  253. BlendOperationRGB: BlendOperationAdd,
  254. BlendOperationAlpha: BlendOperationAdd,
  255. }
  256. // BlendDestinationOver is a preset Blend for Porter Duff's 'destination-over'.
  257. //
  258. // c_out = c_src × (1 - α_dst) + c_dst
  259. // α_out = α_src × (1 - α_dst) + α_dst
  260. BlendDestinationOver = Blend{
  261. BlendFactorSourceRGB: BlendFactorOneMinusDestinationAlpha,
  262. BlendFactorSourceAlpha: BlendFactorOneMinusDestinationAlpha,
  263. BlendFactorDestinationRGB: BlendFactorOne,
  264. BlendFactorDestinationAlpha: BlendFactorOne,
  265. BlendOperationRGB: BlendOperationAdd,
  266. BlendOperationAlpha: BlendOperationAdd,
  267. }
  268. // BlendSourceIn is a preset Blend for Porter Duff's 'source-in'.
  269. //
  270. // c_out = c_src × α_dst
  271. // α_out = α_src × α_dst
  272. BlendSourceIn = Blend{
  273. BlendFactorSourceRGB: BlendFactorDestinationAlpha,
  274. BlendFactorSourceAlpha: BlendFactorDestinationAlpha,
  275. BlendFactorDestinationRGB: BlendFactorZero,
  276. BlendFactorDestinationAlpha: BlendFactorZero,
  277. BlendOperationRGB: BlendOperationAdd,
  278. BlendOperationAlpha: BlendOperationAdd,
  279. }
  280. // BlendDestinationIn is a preset Blend for Porter Duff's 'destination-in'.
  281. //
  282. // c_out = c_dst × α_src
  283. // α_out = α_dst × α_src
  284. BlendDestinationIn = Blend{
  285. BlendFactorSourceRGB: BlendFactorZero,
  286. BlendFactorSourceAlpha: BlendFactorZero,
  287. BlendFactorDestinationRGB: BlendFactorSourceAlpha,
  288. BlendFactorDestinationAlpha: BlendFactorSourceAlpha,
  289. BlendOperationRGB: BlendOperationAdd,
  290. BlendOperationAlpha: BlendOperationAdd,
  291. }
  292. // BlendSourceOut is a preset Blend for Porter Duff's 'source-out'.
  293. //
  294. // c_out = c_src × (1 - α_dst)
  295. // α_out = α_src × (1 - α_dst)
  296. BlendSourceOut = Blend{
  297. BlendFactorSourceRGB: BlendFactorOneMinusDestinationAlpha,
  298. BlendFactorSourceAlpha: BlendFactorOneMinusDestinationAlpha,
  299. BlendFactorDestinationRGB: BlendFactorZero,
  300. BlendFactorDestinationAlpha: BlendFactorZero,
  301. BlendOperationRGB: BlendOperationAdd,
  302. BlendOperationAlpha: BlendOperationAdd,
  303. }
  304. // BlendDestinationOut is a preset Blend for Porter Duff's 'destination-out'.
  305. //
  306. // c_out = c_dst × (1 - α_src)
  307. // α_out = α_dst × (1 - α_src)
  308. BlendDestinationOut = Blend{
  309. BlendFactorSourceRGB: BlendFactorZero,
  310. BlendFactorSourceAlpha: BlendFactorZero,
  311. BlendFactorDestinationRGB: BlendFactorOneMinusSourceAlpha,
  312. BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
  313. BlendOperationRGB: BlendOperationAdd,
  314. BlendOperationAlpha: BlendOperationAdd,
  315. }
  316. // BlendSourceAtop is a preset Blend for Porter Duff's 'source-atop'.
  317. //
  318. // c_out = c_src × α_dst + c_dst × (1 - α_src)
  319. // α_out = α_src × α_dst + α_dst × (1 - α_src)
  320. BlendSourceAtop = Blend{
  321. BlendFactorSourceRGB: BlendFactorDestinationAlpha,
  322. BlendFactorSourceAlpha: BlendFactorDestinationAlpha,
  323. BlendFactorDestinationRGB: BlendFactorOneMinusSourceAlpha,
  324. BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
  325. BlendOperationRGB: BlendOperationAdd,
  326. BlendOperationAlpha: BlendOperationAdd,
  327. }
  328. // BlendDestinationAtop is a preset Blend for Porter Duff's 'destination-atop'.
  329. //
  330. // c_out = c_src × (1 - α_dst) + c_dst × α_src
  331. // α_out = α_src × (1 - α_dst) + α_dst × α_src
  332. BlendDestinationAtop = Blend{
  333. BlendFactorSourceRGB: BlendFactorOneMinusDestinationAlpha,
  334. BlendFactorSourceAlpha: BlendFactorOneMinusDestinationAlpha,
  335. BlendFactorDestinationRGB: BlendFactorSourceAlpha,
  336. BlendFactorDestinationAlpha: BlendFactorSourceAlpha,
  337. BlendOperationRGB: BlendOperationAdd,
  338. BlendOperationAlpha: BlendOperationAdd,
  339. }
  340. // BlendXor is a preset Blend for Porter Duff's 'xor'.
  341. //
  342. // c_out = c_src × (1 - α_dst) + c_dst × (1 - α_src)
  343. // α_out = α_src × (1 - α_dst) + α_dst × (1 - α_src)
  344. BlendXor = Blend{
  345. BlendFactorSourceRGB: BlendFactorOneMinusDestinationAlpha,
  346. BlendFactorSourceAlpha: BlendFactorOneMinusDestinationAlpha,
  347. BlendFactorDestinationRGB: BlendFactorOneMinusSourceAlpha,
  348. BlendFactorDestinationAlpha: BlendFactorOneMinusSourceAlpha,
  349. BlendOperationRGB: BlendOperationAdd,
  350. BlendOperationAlpha: BlendOperationAdd,
  351. }
  352. // BlendLighter is a preset Blend for Porter Duff's 'lighter'.
  353. // This is sum of source and destination (a.k.a. 'plus' or 'additive')
  354. //
  355. // c_out = c_src + c_dst
  356. // α_out = α_src + α_dst
  357. BlendLighter = Blend{
  358. BlendFactorSourceRGB: BlendFactorOne,
  359. BlendFactorSourceAlpha: BlendFactorOne,
  360. BlendFactorDestinationRGB: BlendFactorOne,
  361. BlendFactorDestinationAlpha: BlendFactorOne,
  362. BlendOperationRGB: BlendOperationAdd,
  363. BlendOperationAlpha: BlendOperationAdd,
  364. }
  365. )