1
0

blend.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. func internalBlendFactorToBlendFactor(blendFactor graphicsdriver.BlendFactor) BlendFactor {
  157. switch blendFactor {
  158. case graphicsdriver.BlendFactorZero:
  159. return BlendFactorZero
  160. case graphicsdriver.BlendFactorOne:
  161. return BlendFactorOne
  162. case graphicsdriver.BlendFactorSourceColor:
  163. return BlendFactorSourceColor
  164. case graphicsdriver.BlendFactorOneMinusSourceColor:
  165. return BlendFactorOneMinusSourceColor
  166. case graphicsdriver.BlendFactorSourceAlpha:
  167. return BlendFactorSourceAlpha
  168. case graphicsdriver.BlendFactorOneMinusSourceAlpha:
  169. return BlendFactorOneMinusSourceAlpha
  170. case graphicsdriver.BlendFactorDestinationColor:
  171. return BlendFactorDestinationColor
  172. case graphicsdriver.BlendFactorOneMinusDestinationColor:
  173. return BlendFactorOneMinusDestinationColor
  174. case graphicsdriver.BlendFactorDestinationAlpha:
  175. return BlendFactorDestinationAlpha
  176. case graphicsdriver.BlendFactorOneMinusDestinationAlpha:
  177. return BlendFactorOneMinusDestinationAlpha
  178. default:
  179. panic(fmt.Sprintf("ebiten: invalid blend factor: %d", blendFactor))
  180. }
  181. }
  182. // BlendOperation is an operation for source and destination color values.
  183. type BlendOperation byte
  184. const (
  185. // BlendOperationAdd represents adding the source and destination color.
  186. //
  187. // c_out = (BlendFactorSourceRGB) × c_src + (BlendFactorDestinationRGB) × c_dst
  188. // α_out = (BlendFactorSourceAlpha) × α_src + (BlendFactorDestinationAlpha) × α_dst
  189. BlendOperationAdd BlendOperation = iota
  190. // BlendOperationSubtract represents subtracting the source and destination color.
  191. //
  192. // c_out = (BlendFactorSourceRGB) × c_src - (BlendFactorDestinationRGB) × c_dst
  193. // α_out = (BlendFactorSourceAlpha) × α_src - (BlendFactorDestinationAlpha) × α_dst
  194. BlendOperationSubtract
  195. // BlendOperationReverseSubtract represents subtracting the source and destination color in a reversed order.
  196. //
  197. // c_out = (BlendFactorDestinationRGB) × c_dst - (BlendFactorSourceRGB) × c_src
  198. // α_out = (BlendFactorDestinationAlpha) × α_dst - (BlendFactorSourceAlpha) × α_src
  199. BlendOperationReverseSubtract
  200. // BlendOperationMin represents a minimum function for the source and destination color.
  201. // If BlendOperationMin is specified, blend factors are not used.
  202. //
  203. // c_out = min(c_dst, c_src)
  204. // α_out = min(α_dst, α_src)
  205. BlendOperationMin
  206. // BlendOperationMax represents a maximum function for the source and destination color.
  207. // If BlendOperationMax is specified, blend factors are not used.
  208. //
  209. // c_out = max(c_dst, c_src)
  210. // α_out = max(α_dst, α_src)
  211. BlendOperationMax
  212. )
  213. func (b BlendOperation) internalBlendOperation() graphicsdriver.BlendOperation {
  214. switch b {
  215. case BlendOperationAdd:
  216. return graphicsdriver.BlendOperationAdd
  217. case BlendOperationSubtract:
  218. return graphicsdriver.BlendOperationSubtract
  219. case BlendOperationReverseSubtract:
  220. return graphicsdriver.BlendOperationReverseSubtract
  221. case BlendOperationMin:
  222. return graphicsdriver.BlendOperationMin
  223. case BlendOperationMax:
  224. return graphicsdriver.BlendOperationMax
  225. default:
  226. panic(fmt.Sprintf("ebiten: invalid blend operation: %d", b))
  227. }
  228. }
  229. func internalBlendOperationToBlendOperation(blendOperation graphicsdriver.BlendOperation) BlendOperation {
  230. switch blendOperation {
  231. case graphicsdriver.BlendOperationAdd:
  232. return BlendOperationAdd
  233. case graphicsdriver.BlendOperationSubtract:
  234. return BlendOperationSubtract
  235. case graphicsdriver.BlendOperationReverseSubtract:
  236. return BlendOperationReverseSubtract
  237. case graphicsdriver.BlendOperationMin:
  238. return BlendOperationMin
  239. case graphicsdriver.BlendOperationMax:
  240. return BlendOperationMax
  241. default:
  242. panic(fmt.Sprintf("ebiten: invalid blend operation: %d", blendOperation))
  243. }
  244. }
  245. func internalBlendToBlend(blend graphicsdriver.Blend) Blend {
  246. return Blend{
  247. BlendFactorSourceRGB: internalBlendFactorToBlendFactor(blend.BlendFactorSourceRGB),
  248. BlendFactorSourceAlpha: internalBlendFactorToBlendFactor(blend.BlendFactorSourceAlpha),
  249. BlendFactorDestinationRGB: internalBlendFactorToBlendFactor(blend.BlendFactorDestinationRGB),
  250. BlendFactorDestinationAlpha: internalBlendFactorToBlendFactor(blend.BlendFactorDestinationAlpha),
  251. BlendOperationRGB: internalBlendOperationToBlendOperation(blend.BlendOperationRGB),
  252. BlendOperationAlpha: internalBlendOperationToBlendOperation(blend.BlendOperationAlpha),
  253. }
  254. }
  255. // This name convention follows CSS compositing: https://drafts.fxtf.org/compositing-2/.
  256. //
  257. // In the comments,
  258. // 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.
  259. var (
  260. // BlendSourceOver is a preset Blend for the regular alpha blending.
  261. //
  262. // c_out = c_src + c_dst × (1 - α_src)
  263. // α_out = α_src + α_dst × (1 - α_src)
  264. BlendSourceOver = internalBlendToBlend(graphicsdriver.BlendSourceOver)
  265. // BlendClear is a preset Blend for Porter Duff's 'clear'.
  266. //
  267. // c_out = 0
  268. // α_out = 0
  269. BlendClear = internalBlendToBlend(graphicsdriver.BlendClear)
  270. // BlendCopy is a preset Blend for Porter Duff's 'copy'.
  271. //
  272. // c_out = c_src
  273. // α_out = α_src
  274. BlendCopy = internalBlendToBlend(graphicsdriver.BlendCopy)
  275. // BlendDestination is a preset Blend for Porter Duff's 'destination'.
  276. //
  277. // c_out = c_dst
  278. // α_out = α_dst
  279. BlendDestination = internalBlendToBlend(graphicsdriver.BlendDestination)
  280. // BlendDestinationOver is a preset Blend for Porter Duff's 'destination-over'.
  281. //
  282. // c_out = c_src × (1 - α_dst) + c_dst
  283. // α_out = α_src × (1 - α_dst) + α_dst
  284. BlendDestinationOver = internalBlendToBlend(graphicsdriver.BlendDestinationOver)
  285. // BlendSourceIn is a preset Blend for Porter Duff's 'source-in'.
  286. //
  287. // c_out = c_src × α_dst
  288. // α_out = α_src × α_dst
  289. BlendSourceIn = internalBlendToBlend(graphicsdriver.BlendSourceIn)
  290. // BlendDestinationIn is a preset Blend for Porter Duff's 'destination-in'.
  291. //
  292. // c_out = c_dst × α_src
  293. // α_out = α_dst × α_src
  294. BlendDestinationIn = internalBlendToBlend(graphicsdriver.BlendDestinationIn)
  295. // BlendSourceOut is a preset Blend for Porter Duff's 'source-out'.
  296. //
  297. // c_out = c_src × (1 - α_dst)
  298. // α_out = α_src × (1 - α_dst)
  299. BlendSourceOut = internalBlendToBlend(graphicsdriver.BlendSourceOut)
  300. // BlendDestinationOut is a preset Blend for Porter Duff's 'destination-out'.
  301. //
  302. // c_out = c_dst × (1 - α_src)
  303. // α_out = α_dst × (1 - α_src)
  304. BlendDestinationOut = internalBlendToBlend(graphicsdriver.BlendDestinationOut)
  305. // BlendSourceAtop is a preset Blend for Porter Duff's 'source-atop'.
  306. //
  307. // c_out = c_src × α_dst + c_dst × (1 - α_src)
  308. // α_out = α_src × α_dst + α_dst × (1 - α_src)
  309. BlendSourceAtop = internalBlendToBlend(graphicsdriver.BlendSourceAtop)
  310. // BlendDestinationAtop is a preset Blend for Porter Duff's 'destination-atop'.
  311. //
  312. // c_out = c_src × (1 - α_dst) + c_dst × α_src
  313. // α_out = α_src × (1 - α_dst) + α_dst × α_src
  314. BlendDestinationAtop = internalBlendToBlend(graphicsdriver.BlendDestinationAtop)
  315. // BlendXor is a preset Blend for Porter Duff's 'xor'.
  316. //
  317. // c_out = c_src × (1 - α_dst) + c_dst × (1 - α_src)
  318. // α_out = α_src × (1 - α_dst) + α_dst × (1 - α_src)
  319. BlendXor = internalBlendToBlend(graphicsdriver.BlendXor)
  320. // BlendLighter is a preset Blend for Porter Duff's 'lighter'.
  321. // This is sum of source and destination (a.k.a. 'plus' or 'additive')
  322. //
  323. // c_out = c_src + c_dst
  324. // α_out = α_src + α_dst
  325. BlendLighter = internalBlendToBlend(graphicsdriver.BlendLighter)
  326. )