BsPixelData.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Image/BsPixelVolume.h"
  6. #include "Resources/BsGpuResourceData.h"
  7. #include "Reflection/BsIReflectable.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Resources
  11. * @{
  12. */
  13. /** Pixel formats usable by images, textures and render surfaces. */
  14. enum BS_SCRIPT_EXPORT() PixelFormat
  15. {
  16. /** Unknown pixel format. */
  17. PF_UNKNOWN BS_SCRIPT_EXPORT(ex:true) = 0,
  18. /** 8-bit 1-channel pixel format, unsigned normalized. */
  19. PF_R8 BS_SCRIPT_EXPORT(n:R8) = 1,
  20. /** 8-bit 2-channel pixel format, unsigned normalized. */
  21. PF_RG8 BS_SCRIPT_EXPORT(n:RG8) = 2,
  22. /** 8-bit 3-channel pixel format, unsigned normalized. */
  23. PF_RGB8 BS_SCRIPT_EXPORT(n:RGB8) = 3,
  24. /** 8-bit 3-channel pixel format, unsigned normalized. */
  25. PF_BGR8 BS_SCRIPT_EXPORT(n:BGR8) = 4,
  26. /** 8-bit 4-channel pixel format, unsigned normalized. */
  27. PF_BGRA8 BS_SCRIPT_EXPORT(n:BGRA8) = 7,
  28. /** 8-bit 4-channel pixel format, unsigned normalized. */
  29. PF_RGBA8 BS_SCRIPT_EXPORT(n:RGBA8) = 8,
  30. /** DXT1/BC1 format containing opaque RGB or 1-bit alpha RGB. 4 bits per pixel. */
  31. PF_BC1 BS_SCRIPT_EXPORT(n:BC1) = 13,
  32. /** DXT3/BC2 format containing RGB with premultiplied alpha. 4 bits per pixel. */
  33. PF_BC1a BS_SCRIPT_EXPORT(ex:true) = 14,
  34. /** DXT3/BC2 format containing RGB with explicit alpha. 8 bits per pixel. */
  35. PF_BC2 BS_SCRIPT_EXPORT(n:BC2) = 15,
  36. /** DXT5/BC2 format containing RGB with explicit alpha. 8 bits per pixel. Better alpha gradients than BC2. */
  37. PF_BC3 BS_SCRIPT_EXPORT(n:BC3) = 16,
  38. /** One channel compressed format. 4 bits per pixel. */
  39. PF_BC4 BS_SCRIPT_EXPORT(n:BC4) = 17,
  40. /** Two channel compressed format. 8 bits per pixel. */
  41. PF_BC5 BS_SCRIPT_EXPORT(n:BC5) = 18,
  42. /** Format storing RGB in half (16-bit) floating point format usable for HDR. 8 bits per pixel. */
  43. PF_BC6H BS_SCRIPT_EXPORT(n:BC6H) = 19,
  44. /**
  45. * Format storing RGB with optional alpha channel. Similar to BC1/BC2/BC3 formats but with higher quality and
  46. * higher decompress overhead. 8 bits per pixel.
  47. */
  48. PF_BC7 BS_SCRIPT_EXPORT(n:BC7) = 20,
  49. /** 16-bit 1-channel pixel format, signed float. */
  50. PF_R16F BS_SCRIPT_EXPORT(n:R16F) = 21,
  51. /** 16-bit 2-channel pixel format, signed float. */
  52. PF_RG16F BS_SCRIPT_EXPORT(n:RG16F) = 22,
  53. /** 16-bit 4-channel pixel format, signed float. */
  54. PF_RGBA16F BS_SCRIPT_EXPORT(n:RGBA16F) = 24,
  55. /** 32-bit 1-channel pixel format, signed float. */
  56. PF_R32F BS_SCRIPT_EXPORT(n:R32F) = 25,
  57. /** 32-bit 2-channel pixel format, signed float. */
  58. PF_RG32F BS_SCRIPT_EXPORT(n:RG32F) = 26,
  59. /** 32-bit 3-channel pixel format, signed float. */
  60. PF_RGB32F BS_SCRIPT_EXPORT(n:RGB32F) = 27,
  61. /** 32-bit 4-channel pixel format, signed float. */
  62. PF_RGBA32F BS_SCRIPT_EXPORT(n:RGBA32F) = 28,
  63. /** Depth stencil format, 32bit depth, 8bit stencil + 24 unused. Depth stored as signed float. */
  64. PF_D32_S8X24 BS_SCRIPT_EXPORT(n:D32_S8X24) = 29,
  65. /** Depth stencil fomrat, 24bit depth + 8bit stencil. Depth stored as unsigned normalized. */
  66. PF_D24S8 BS_SCRIPT_EXPORT(n:D24S8) = 30,
  67. /** Depth format, 32bits. Signed float. */
  68. PF_D32 BS_SCRIPT_EXPORT(n:D32) = 31,
  69. /** Depth format, 16bits. Unsigned normalized. */
  70. PF_D16 BS_SCRIPT_EXPORT(n:D16) = 32,
  71. /** Packed unsigned float format, 11 bits for red, 11 bits for green, 10 bits for blue. */
  72. PF_RG11B10F BS_SCRIPT_EXPORT(ex:true) = 33,
  73. /**
  74. * Packed unsigned normalized format, 10 bits for red, 10 bits for green, 10 bits for blue, and two bits for alpha.
  75. */
  76. PF_RGB10A2 BS_SCRIPT_EXPORT(ex:true) = 34,
  77. /** 8-bit 1-channel pixel format, signed integer. */
  78. PF_R8I BS_SCRIPT_EXPORT(n:R8I) = 35,
  79. /** 8-bit 2-channel pixel format, signed integer. */
  80. PF_RG8I BS_SCRIPT_EXPORT(n:RG8I) = 36,
  81. /** 8-bit 4-channel pixel format, signed integer. */
  82. PF_RGBA8I BS_SCRIPT_EXPORT(n:RGBA8I) = 37,
  83. /** 8-bit 1-channel pixel format, unsigned integer. */
  84. PF_R8U BS_SCRIPT_EXPORT(n:R8U) = 38,
  85. /** 8-bit 2-channel pixel format, unsigned integer. */
  86. PF_RG8U BS_SCRIPT_EXPORT(n:RG8U) = 39,
  87. /** 8-bit 4-channel pixel format, unsigned integer. */
  88. PF_RGBA8U BS_SCRIPT_EXPORT(n:RGBA8U) = 40,
  89. /** 8-bit 1-channel pixel format, signed normalized. */
  90. PF_R8S BS_SCRIPT_EXPORT(n:R8S) = 41,
  91. /** 8-bit 2-channel pixel format, signed normalized. */
  92. PF_RG8S BS_SCRIPT_EXPORT(n:RG8S) = 42,
  93. /** 8-bit 4-channel pixel format, signed normalized. */
  94. PF_RGBA8S BS_SCRIPT_EXPORT(n:RGBA8S) = 43,
  95. /** 16-bit 1-channel pixel format, signed integer. */
  96. PF_R16I BS_SCRIPT_EXPORT(n:R16I) = 44,
  97. /** 16-bit 2-channel pixel format, signed integer. */
  98. PF_RG16I BS_SCRIPT_EXPORT(n:RG16I) = 45,
  99. /** 16-bit 4-channel pixel format, signed integer. */
  100. PF_RGBA16I BS_SCRIPT_EXPORT(n:RGBA16I) = 46,
  101. /** 16-bit 1-channel pixel format, unsigned integer. */
  102. PF_R16U BS_SCRIPT_EXPORT(n:R16U) = 47,
  103. /** 16-bit 2-channel pixel format, unsigned integer. */
  104. PF_RG16U BS_SCRIPT_EXPORT(n:RG16U) = 48,
  105. /** 16-bit 4-channel pixel format, unsigned integer. */
  106. PF_RGBA16U BS_SCRIPT_EXPORT(n:RGBA16U) = 49,
  107. /** 32-bit 1-channel pixel format, signed integer. */
  108. PF_R32I BS_SCRIPT_EXPORT(n:R32I) = 50,
  109. /** 32-bit 2-channel pixel format, signed integer. */
  110. PF_RG32I BS_SCRIPT_EXPORT(n:RG32I) = 51,
  111. /** 32-bit 3-channel pixel format, signed integer. */
  112. PF_RGB32I BS_SCRIPT_EXPORT(n:RGB32I) = 52,
  113. /** 32-bit 4-channel pixel format, signed integer. */
  114. PF_RGBA32I BS_SCRIPT_EXPORT(n:RGBA32I) = 53,
  115. /** 32-bit 1-channel pixel format, unsigned integer. */
  116. PF_R32U BS_SCRIPT_EXPORT(n:R32U) = 54,
  117. /** 32-bit 2-channel pixel format, unsigned integer. */
  118. PF_RG32U BS_SCRIPT_EXPORT(n:RG32U) = 55,
  119. /** 32-bit 3-channel pixel format, unsigned integer. */
  120. PF_RGB32U BS_SCRIPT_EXPORT(n:RGB32U) = 56,
  121. /** 32-bit 4-channel pixel format, unsigned integer. */
  122. PF_RGBA32U BS_SCRIPT_EXPORT(n:RGBA32U) = 57,
  123. /** 16-bit 1-channel pixel format, signed normalized. */
  124. PF_R16S BS_SCRIPT_EXPORT(n:R16S) = 58,
  125. /** 16-bit 2-channel pixel format, signed normalized. */
  126. PF_RG16S BS_SCRIPT_EXPORT(n:RG16S) = 59,
  127. /** 16-bit 4-channel pixel format, signed normalized. */
  128. PF_RGBA16S BS_SCRIPT_EXPORT(n:RGBA16S) = 60,
  129. /** 16-bit 1-channel pixel format, unsigned normalized. */
  130. PF_R16 BS_SCRIPT_EXPORT(n:R16) = 61,
  131. /** 16-bit 2-channel pixel format, unsigned normalized. */
  132. PF_RG16 BS_SCRIPT_EXPORT(n:RG16) = 62,
  133. /** 16-bit 3-channel pixel format, unsigned normalized. */
  134. PF_RGB16 BS_SCRIPT_EXPORT(n:RGB16) = 63,
  135. /** 16-bit 4-channel pixel format, unsigned normalized. */
  136. PF_RGBA16 BS_SCRIPT_EXPORT(n:RGBA16) = 64,
  137. /** Number of pixel formats currently defined. */
  138. PF_COUNT BS_SCRIPT_EXPORT(ex:true)
  139. };
  140. /** Flags defining some properties of pixel formats. */
  141. enum PixelFormatFlags {
  142. /** This format has an alpha channel. */
  143. PFF_HASALPHA = 0x1,
  144. /**
  145. * This format is compressed. This invalidates the values in elemBytes, elemBits and the bit counts as these might
  146. * not be fixed in a compressed format.
  147. */
  148. PFF_COMPRESSED = 0x2,
  149. /** This is a floating point format. */
  150. PFF_FLOAT = 0x4,
  151. /** This is a depth format (for depth textures). */
  152. PFF_DEPTH = 0x8,
  153. /** This format stores data internally as integers. */
  154. PFF_INTEGER = 0x10,
  155. /** Format contains signed data. Absence of this flag implies unsigned data. */
  156. PFF_SIGNED = 0x20,
  157. /** Format contains normalized data. This will be [0, 1] for unsigned, and [-1,1] for signed formats. */
  158. PFF_NORMALIZED = 0x40
  159. };
  160. /** Types used for individual components of a pixel. */
  161. enum PixelComponentType
  162. {
  163. PCT_BYTE = 0, /**< 8-bit integer per component */
  164. PCT_SHORT = 1, /**< 16-bit integer per component. */
  165. PCT_INT = 2, /**< 32-bit integer per component. */
  166. PCT_FLOAT16 = 3, /**< 16 bit float per component */
  167. PCT_FLOAT32 = 4, /**< 32 bit float per component */
  168. PCT_PACKED_R11G11B10 = 5, /**< 11 bits for first two components, 10 for third component. */
  169. PCT_PACKED_R10G10B10A2 = 6, /**< 10 bits for first three components, 2 bits for last component */
  170. PCT_COUNT /**< Number of pixel types */
  171. };
  172. /** Determines how are texture pixels filtered during sampling. */
  173. enum TextureFilter
  174. {
  175. /** Pixel nearest to the sampled location is chosen. */
  176. TF_NEAREST,
  177. /** Four pixels nearest to the sampled location are interpolated to yield the sampled color. */
  178. TF_BILINEAR
  179. };
  180. /** A list of cubemap faces. */
  181. enum CubemapFace
  182. {
  183. CF_PositiveX,
  184. CF_NegativeX,
  185. CF_PositiveY,
  186. CF_NegativeY,
  187. CF_PositiveZ,
  188. CF_NegativeZ
  189. };
  190. /**
  191. * A buffer describing a volume (3D), image (2D) or line (1D) of pixels in memory. Pixels are stored as a succession
  192. * of "depth" slices, each containing "height" rows of "width" pixels.
  193. *
  194. * @note
  195. * If using the constructor instead of create() you must call GpuResourceData::allocateInternalBuffer or set the buffer
  196. * in some other way before reading/writing from this object, as by the default there is no buffer allocated.
  197. *
  198. * @see GpuResourceData
  199. */
  200. class BS_CORE_EXPORT BS_SCRIPT_EXPORT() PixelData : public GpuResourceData
  201. {
  202. public:
  203. PixelData();
  204. ~PixelData() {}
  205. /**
  206. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  207. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  208. */
  209. PixelData(const PixelVolume& extents, PixelFormat pixelFormat);
  210. /**
  211. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  212. * height and depth, where each pixel is of the specified pixel format.
  213. */
  214. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat);
  215. PixelData(const PixelData& copy);
  216. PixelData& operator=(const PixelData& rhs);
  217. /**
  218. * Returns the number of pixels that offsets one row from another. This can be "width", but doesn't have to be as
  219. * some buffers require padding.
  220. */
  221. BS_SCRIPT_EXPORT(n:RawRowPitch,pr:getter)
  222. UINT32 getRowPitch() const { return mRowPitch; }
  223. /**
  224. * Returns the number of pixels that offsets one depth slice from another. This can be "width * height", but
  225. * doesn't have to be as some buffers require padding.
  226. */
  227. BS_SCRIPT_EXPORT(n:RawSlicePitch,pr:getter)
  228. UINT32 getSlicePitch() const { return mSlicePitch; }
  229. /**
  230. * Sets the pitch (in pixels) that determines offset between rows of the pixel buffer. Call this before allocating
  231. * the buffer.
  232. */
  233. void setRowPitch(UINT32 rowPitch) { mRowPitch = rowPitch; }
  234. /**
  235. * Sets the pitch (in pixels) that determines offset between depth slices of the pixel buffer. Call this before
  236. * allocating the buffer.
  237. */
  238. void setSlicePitch(UINT32 slicePitch) { mSlicePitch = slicePitch; }
  239. /**
  240. * Returns the number of extra pixels in a row (non-zero only if rows are not consecutive (row pitch is larger
  241. * than width)).
  242. */
  243. UINT32 getRowSkip() const { return mRowPitch - getWidth(); }
  244. /**
  245. * Returns the number of extra pixels in a depth slice (non-zero only if slices aren't consecutive (slice pitch is
  246. * larger than width*height).
  247. */
  248. UINT32 getSliceSkip() const { return mSlicePitch - (getHeight() * mRowPitch); }
  249. /** Returns the pixel format used by the internal buffer for storing the pixels. */
  250. BS_SCRIPT_EXPORT(n:Format,pr:getter)
  251. PixelFormat getFormat() const { return mFormat; }
  252. /** Returns width of the buffer in pixels. */
  253. UINT32 getWidth() const { return mExtents.getWidth(); }
  254. /** Returns height of the buffer in pixels. */
  255. UINT32 getHeight() const { return mExtents.getHeight(); }
  256. /** Returns depth of the buffer in pixels. */
  257. UINT32 getDepth() const { return mExtents.getDepth(); }
  258. /**
  259. * Returns left-most start of the pixel volume. This value is not used internally in any way. It is just passed
  260. * through from the constructor.
  261. */
  262. UINT32 getLeft() const { return mExtents.left; }
  263. /**
  264. * Returns right-most end of the pixel volume. This value is not used internally in any way. It is just passed
  265. * through from the constructor.
  266. */
  267. UINT32 getRight() const { return mExtents.right; }
  268. /**
  269. * Returns top-most start of the pixel volume. This value is not used internally in any way. It is just passed
  270. * through from the constructor.
  271. */
  272. UINT32 getTop() const { return mExtents.top; }
  273. /**
  274. * Returns bottom-most end of the pixel volume. This value is not used internally in any way. It is just passed
  275. * through from the constructor.
  276. */
  277. UINT32 getBottom() const { return mExtents.bottom; }
  278. /**
  279. * Returns front-most start of the pixel volume. This value is not used internally in any way. It is just passed
  280. * through from the constructor.
  281. */
  282. UINT32 getFront() const { return mExtents.front; }
  283. /**
  284. * Returns back-most end of the pixel volume. This value is not used internally in any way. It is just passed
  285. * through from the constructor.
  286. */
  287. UINT32 getBack() const { return mExtents.back; }
  288. /** Returns extents of the pixel volume this object is capable of holding. */
  289. BS_SCRIPT_EXPORT(n:Extents,pr:getter)
  290. PixelVolume getExtents() const { return mExtents; }
  291. /**
  292. * Return whether this buffer is laid out consecutive in memory (meaning the pitches are equal to the dimensions).
  293. */
  294. BS_SCRIPT_EXPORT(n:RawIsConsecutive,pr:getter)
  295. bool isConsecutive() const
  296. {
  297. return mRowPitch == getWidth() && mSlicePitch == getWidth()*getHeight();
  298. }
  299. /** Return the size (in bytes) this image would take if it was laid out consecutive in memory. */
  300. UINT32 getConsecutiveSize() const;
  301. /** Return the size (in bytes) of the buffer this image requires. */
  302. BS_SCRIPT_EXPORT(n:RawSize,pr:getter)
  303. UINT32 getSize() const;
  304. /**
  305. * Returns pixel data containing a sub-volume of this object. Returned data will not have its own buffer, but will
  306. * instead point to this one. It is up to the caller to ensure this object outlives any sub-volume objects.
  307. */
  308. PixelData getSubVolume(const PixelVolume& volume) const;
  309. /**
  310. * Samples a color at the specified coordinates using a specific filter.
  311. *
  312. * @param[in] coords Coordinates to sample the color at. They start at top left corner (0, 0), and are in range
  313. * [0, 1].
  314. * @param[in] filter Filtering mode to use when sampling the color.
  315. * @return Sampled color.
  316. */
  317. Color sampleColorAt(const Vector2& coords, TextureFilter filter = TF_BILINEAR) const;
  318. /** Returns pixel color at the specified coordinates. */
  319. Color getColorAt(UINT32 x, UINT32 y, UINT32 z = 0) const;
  320. /** Sets the pixel color at the specified coordinates. */
  321. void setColorAt(const Color& color, UINT32 x, UINT32 y, UINT32 z = 0);
  322. /**
  323. * Converts all the internal data into an array of colors. Array is mapped as such:
  324. * arrayIdx = x + y * width + z * width * height.
  325. */
  326. Vector<Color> getColors() const;
  327. /**
  328. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  329. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  330. */
  331. void setColors(const Vector<Color>& colors);
  332. /**
  333. * Initializes the internal buffer with the provided set of colors. The array should be of
  334. * width * height * depth size and mapped as such: arrayIdx = x + y * width + z * width * height.
  335. */
  336. void setColors(Color* colors, UINT32 numElements);
  337. /**
  338. * Interprets pixel data as depth information as retrieved from the GPU's depth buffer. Converts the device specific
  339. * depth value to range [0, 1] and returns it.
  340. */
  341. float getDepthAt(UINT32 x, UINT32 y, UINT32 z = 0) const;
  342. /**
  343. * Converts all the internal data into an array of floats as if each individual pixel is retrieved with
  344. * getDepthAt(). Array is mapped as such: arrayIdx = x + y * width + z * width * height.
  345. */
  346. Vector<float> getDepths() const;
  347. /**
  348. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  349. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  350. */
  351. static SPtr<PixelData> create(const PixelVolume& extents, PixelFormat pixelFormat);
  352. /**
  353. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  354. * height and depth, where each pixel is of the specified pixel format.
  355. */
  356. static SPtr<PixelData> create(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat);
  357. private:
  358. /**
  359. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  360. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  361. *
  362. * @note A generic method that is reused in other more specific setColors() calls.
  363. */
  364. template<class T>
  365. void setColorsInternal(const T& colors, UINT32 numElements);
  366. /** Returns the needed size of the internal buffer, in bytes. */
  367. UINT32 getInternalBufferSize() const override;
  368. private:
  369. PixelVolume mExtents;
  370. PixelFormat mFormat;
  371. UINT32 mRowPitch;
  372. UINT32 mSlicePitch;
  373. /************************************************************************/
  374. /* SERIALIZATION */
  375. /************************************************************************/
  376. public:
  377. friend class PixelDataRTTI;
  378. static RTTITypeBase* getRTTIStatic();
  379. RTTITypeBase* getRTTI() const override;
  380. };
  381. /** @} */
  382. }