BsPixelData.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "BsPixelVolume.h"
  6. #include "BsGpuResourceData.h"
  7. #include "BsIReflectable.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup Resources
  11. * @{
  12. */
  13. /** Pixel formats usable by images, textures and render surfaces. */
  14. enum PixelFormat
  15. {
  16. /** Unknown pixel format. */
  17. PF_UNKNOWN = 0,
  18. /** 8-bit pixel format, all bits red. */
  19. PF_R8 = 1,
  20. /** 2 byte pixel format, 1 byte red, 1 byte green. */
  21. PF_R8G8 = 2,
  22. /** 24-bit pixel format, 8 bits for red, green and blue. */
  23. PF_R8G8B8 = 3,
  24. /** 24-bit pixel format, 8 bits for blue, green and red. */
  25. PF_B8G8R8 = 4,
  26. /** 32-bit pixel format, 8 bits for alpha, red, green and blue. */
  27. PF_A8R8G8B8 = 5,
  28. /** 32-bit pixel format, 8 bits for blue, green, red and alpha. */
  29. PF_A8B8G8R8 = 6,
  30. /** 32-bit pixel format, 8 bits for blue, green, red and alpha. */
  31. PF_B8G8R8A8 = 7,
  32. /** 32-bit pixel format, 8 bits for red, green, blue and alpha. */
  33. PF_R8G8B8A8 = 8,
  34. /**
  35. * 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue. Like PF_A8R8G8B8, but alpha will get
  36. * discarded.
  37. */
  38. PF_X8R8G8B8 = 9,
  39. /**
  40. * 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red. Like PF_A8B8G8R8, but alpha will get
  41. * discarded.
  42. */
  43. PF_X8B8G8R8 = 10,
  44. /**
  45. * 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue. Like PF_R8G8B8A8, but alpha will get
  46. * discarded.
  47. */
  48. PF_R8G8B8X8 = 11,
  49. /** 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red. */
  50. /** Like PF_B8G8R8A8, but alpha will get discarded. */
  51. PF_B8G8R8X8 = 12,
  52. /** 24-bit pixel format, 8 bits for red, green and blue. */
  53. PF_BYTE_RGB = PF_R8G8B8,
  54. /** 24-bit pixel format, 8 bits for blue, green and red. */
  55. PF_BYTE_BGR = PF_B8G8R8,
  56. /** 32-bit pixel format, 8 bits for blue, green, red and alpha. */
  57. PF_BYTE_BGRA = PF_B8G8R8A8,
  58. /** 32-bit pixel format, 8 bits for red, green, blue and alpha. */
  59. PF_BYTE_RGBA = PF_R8G8B8A8,
  60. /** DXT1/BC1 format containing opaque RGB or 1-bit alpha RGB. 4 bits per pixel. */
  61. PF_BC1 = 13,
  62. /** DXT3/BC2 format containing RGB with premultiplied alpha. 4 bits per pixel. */
  63. PF_BC1a = 14,
  64. /** DXT3/BC2 format containing RGB with explicit alpha. 8 bits per pixel. */
  65. PF_BC2 = 15,
  66. /** DXT5/BC2 format containing RGB with explicit alpha. 8 bits per pixel. Better alpha gradients than BC2. */
  67. PF_BC3 = 16,
  68. /** One channel compressed format. 4 bits per pixel. */
  69. PF_BC4 = 17,
  70. /** Two channel compressed format. 8 bits per pixel. */
  71. PF_BC5 = 18,
  72. /** Format storing RGB in half (16-bit) floating point format usable for HDR. 8 bits per pixel. */
  73. PF_BC6H = 19,
  74. /**
  75. * Format storing RGB with optional alpha channel. Similar to BC1/BC2/BC3 formats but with higher quality and
  76. * higher decompress overhead. 8 bits per pixel.
  77. */
  78. PF_BC7 = 20,
  79. /** 16-bit pixel format, 16 bits (float) for red. */
  80. PF_FLOAT16_R = 21,
  81. /** 32-bit, 2-channel s10e5 floating point pixel format, 16-bit red, 16-bit green. */
  82. PF_FLOAT16_RG = 22,
  83. /** 48-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue. */
  84. PF_FLOAT16_RGB = 23,
  85. /**
  86. * 64-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue, 16 bits
  87. * (float) for alpha.
  88. */
  89. PF_FLOAT16_RGBA = 24,
  90. /** 32-bit pixel format, 32 bits (float) for red. */
  91. PF_FLOAT32_R = 25,
  92. /** 64-bit, 2-channel floating point pixel format, 32-bit red, 32-bit green. */
  93. PF_FLOAT32_RG = 26,
  94. /** 96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue. */
  95. PF_FLOAT32_RGB = 27,
  96. /**
  97. * 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits
  98. * (float) for alpha.
  99. */
  100. PF_FLOAT32_RGBA = 28,
  101. /** Depth stencil format, 32bit depth, 8bit stencil + 24 unused. */
  102. PF_D32_S8X24 = 29,
  103. /** Depth stencil fomrat, 24bit depth + 8bit stencil. */
  104. PF_D24S8 = 30,
  105. /** Depth format, 32bits. */
  106. PF_D32 = 31,
  107. /** Depth format, 16bits. */
  108. PF_D16 = 32,
  109. /**
  110. * 32-bit float format, 11 bits (float) for red, 11 bits (float) for green, 10 bits (float) for blue. Framebuffer
  111. * only format, not for CPU use.
  112. */
  113. PF_FLOAT_R11G11B10 = 33,
  114. /**
  115. * 32-bit unsigned normalized format, 10 bits (float) for red, 10 bits (float) for green, 10 bits (float) for blue,
  116. * and two bits for alpha. Framebuffer only format, not for CPU use.
  117. */
  118. PF_UNORM_R10G10B10A2 = 34,
  119. /** Number of pixel formats currently defined. */
  120. PF_COUNT = 35
  121. };
  122. typedef Vector<PixelFormat> PixelFormatList;
  123. /** Flags defining some properties of pixel formats. */
  124. enum PixelFormatFlags {
  125. /** This format has an alpha channel. */
  126. PFF_HASALPHA = 0x00000001,
  127. /**
  128. * This format is compressed. This invalidates the values in elemBytes, elemBits and the bit counts as these might
  129. * not be fixed in a compressed format.
  130. */
  131. PFF_COMPRESSED = 0x00000002,
  132. /** This is a floating point format. */
  133. PFF_FLOAT = 0x00000004,
  134. /** This is a depth format (for depth textures). */
  135. PFF_DEPTH = 0x00000008,
  136. /**
  137. * Format is in native endian. Generally true for the 16, 24 and 32 bits formats which can be represented as
  138. * machine integers.
  139. */
  140. PFF_NATIVEENDIAN = 0x00000010
  141. };
  142. /** Types used for individual components of a pixel. */
  143. enum PixelComponentType
  144. {
  145. PCT_BYTE = 0, /**< Byte per component */
  146. PCT_SHORT = 1, /**< Short per component */
  147. PCT_FLOAT16 = 2, /**< 16 bit float per component */
  148. PCT_FLOAT32 = 3, /**< 32 bit float per component */
  149. PCT_PACKED_R11G11B10 = 4, /**< 11 bits for first two components, 10 for third component. */
  150. PCT_PACKED_R10G10B10A2 = 5, /**< 10 bits for first three components, 2 bits for last component */
  151. PCT_COUNT = 4 /**< Number of pixel types */
  152. };
  153. /**
  154. * A buffer describing a volume (3D), image (2D) or line (1D) of pixels in memory. Pixels are stored as a succession
  155. * of "depth" slices, each containing "height" rows of "width" pixels.
  156. *
  157. * @note
  158. * If using the constructor instead of create() you must call GpuResourceData::allocateInternalBuffer or set the buffer
  159. * in some other way before reading/writing from this object, as by the default there is no buffer allocated.
  160. *
  161. * @see GpuResourceData
  162. */
  163. class BS_CORE_EXPORT PixelData : public GpuResourceData
  164. {
  165. public:
  166. PixelData() {}
  167. ~PixelData() {}
  168. /**
  169. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  170. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  171. */
  172. PixelData(const PixelVolume& extents, PixelFormat pixelFormat)
  173. :mExtents(extents), mFormat(pixelFormat)
  174. {
  175. setConsecutive();
  176. }
  177. /**
  178. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  179. * height and depth, where each pixel is of the specified pixel format.
  180. */
  181. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat)
  182. : mExtents(0, 0, 0, width, height, depth), mFormat(pixelFormat)
  183. {
  184. setConsecutive();
  185. }
  186. PixelData(const PixelData& copy);
  187. PixelData& operator=(const PixelData& rhs);
  188. /**
  189. * Returns the number of pixels that offsets one row from another. This can be "width", but doesn't have to be as
  190. * some buffers require padding.
  191. */
  192. UINT32 getRowPitch() const { return mRowPitch; }
  193. /**
  194. * Returns the number of pixels that offsets one depth slice from another. This can be "width * height", but
  195. * doesn't have to be as some buffers require padding.
  196. */
  197. UINT32 getSlicePitch() const { return mSlicePitch; }
  198. /**
  199. * Sets the pitch (in pixels) that determines offset between rows of the pixel buffer. Call this before allocating
  200. * the buffer.
  201. */
  202. void setRowPitch(UINT32 rowPitch) { mRowPitch = rowPitch; }
  203. /**
  204. * Sets the pitch (in pixels) that determines offset between depth slices of the pixel buffer. Call this before
  205. * allocating the buffer.
  206. */
  207. void setSlicePitch(UINT32 slicePitch) { mSlicePitch = slicePitch; }
  208. /**
  209. * Returns the number of extra pixels in a row (non-zero only if rows are not consecutive (row pitch is larger
  210. * than width)).
  211. */
  212. UINT32 getRowSkip() const { return mRowPitch - getWidth(); }
  213. /**
  214. * Returns the number of extra pixels in a depth slice (non-zero only if slices aren't consecutive (slice pitch is
  215. * larger than width*height).
  216. */
  217. UINT32 getSliceSkip() const { return mSlicePitch - (getHeight() * mRowPitch); }
  218. /** Returns the pixel format used by the internal buffer for storing the pixels. */
  219. PixelFormat getFormat() const { return mFormat; }
  220. /** Returns width of the buffer in pixels. */
  221. UINT32 getWidth() const { return mExtents.getWidth(); }
  222. /** Returns height of the buffer in pixels. */
  223. UINT32 getHeight() const { return mExtents.getHeight(); }
  224. /** Returns depth of the buffer in pixels. */
  225. UINT32 getDepth() const { return mExtents.getDepth(); }
  226. /**
  227. * Returns left-most start of the pixel volume. This value is not used internally in any way. It is just passed
  228. * through from the constructor.
  229. */
  230. UINT32 getLeft() const { return mExtents.left; }
  231. /**
  232. * Returns right-most end of the pixel volume. This value is not used internally in any way. It is just passed
  233. * through from the constructor.
  234. */
  235. UINT32 getRight() const { return mExtents.right; }
  236. /**
  237. * Returns top-most start of the pixel volume. This value is not used internally in any way. It is just passed
  238. * through from the constructor.
  239. */
  240. UINT32 getTop() const { return mExtents.top; }
  241. /**
  242. * Returns bottom-most end of the pixel volume. This value is not used internally in any way. It is just passed
  243. * through from the constructor.
  244. */
  245. UINT32 getBottom() const { return mExtents.bottom; }
  246. /**
  247. * Returns front-most start of the pixel volume. This value is not used internally in any way. It is just passed
  248. * through from the constructor.
  249. */
  250. UINT32 getFront() const { return mExtents.front; }
  251. /**
  252. * Returns back-most end of the pixel volume. This value is not used internally in any way. It is just passed
  253. * through from the constructor.
  254. */
  255. UINT32 getBack() const { return mExtents.back; }
  256. /** Returns extents of the pixel volume this object is capable of holding. */
  257. PixelVolume getExtents() const { return mExtents; }
  258. /**
  259. * Return whether this buffer is laid out consecutive in memory (meaning the pitches are equal to the dimensions).
  260. */
  261. bool isConsecutive() const
  262. {
  263. return mRowPitch == getWidth() && mSlicePitch == getWidth()*getHeight();
  264. }
  265. /** Return the size (in bytes) this image would take if it was laid out consecutive in memory. */
  266. UINT32 getConsecutiveSize() const;
  267. /** Return the size (in bytes) of the buffer this image requires. */
  268. UINT32 getSize() const;
  269. /**
  270. * Returns pixel data containing a sub-volume of this object. Returned data will not have its own buffer, but will
  271. * instead point to this one. It is up to the caller to ensure this object outlives any sub-volume objects.
  272. */
  273. PixelData getSubVolume(const PixelVolume &def) const;
  274. /** Returns pixel color at the specified coordinates. */
  275. Color getColorAt(UINT32 x, UINT32 y, UINT32 z = 0) const;
  276. /** Sets the pixel color at the specified coordinates. */
  277. void setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z = 0);
  278. /**
  279. * Converts all the internal data into an array of colors. Array is mapped as such:
  280. * arrayIdx = x + y * width + z * width * height.
  281. */
  282. Vector<Color> getColors() const;
  283. /**
  284. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  285. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  286. */
  287. void setColors(const Vector<Color>& colors);
  288. /**
  289. * Initializes the internal buffer with the provided set of colors. The array should be of
  290. * width * height * depth size and mapped as such: arrayIdx = x + y * width + z * width * height.
  291. */
  292. void setColors(Color* colors, UINT32 numElements);
  293. /**
  294. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  295. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  296. */
  297. static SPtr<PixelData> create(const PixelVolume &extents, PixelFormat pixelFormat);
  298. /**
  299. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  300. * height and depth, where each pixel is of the specified pixel format.
  301. */
  302. static SPtr<PixelData> create(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat);
  303. private:
  304. /**
  305. * Set the rowPitch and slicePitch so that the buffer is laid out consecutive in memory. Does not actually modify
  306. * the buffer itself.
  307. */
  308. void setConsecutive()
  309. {
  310. mRowPitch = getWidth();
  311. mSlicePitch = getWidth()*getHeight();
  312. }
  313. /**
  314. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  315. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  316. *
  317. * @note A generic method that is reused in other more specific setColors() calls.
  318. */
  319. template<class T>
  320. void setColorsInternal(const T& colors, UINT32 numElements);
  321. /** Returns the needed size of the internal buffer, in bytes. */
  322. UINT32 getInternalBufferSize() const override;
  323. private:
  324. PixelVolume mExtents;
  325. PixelFormat mFormat;
  326. UINT32 mRowPitch;
  327. UINT32 mSlicePitch;
  328. /************************************************************************/
  329. /* SERIALIZATION */
  330. /************************************************************************/
  331. public:
  332. friend class PixelDataRTTI;
  333. static RTTITypeBase* getRTTIStatic();
  334. virtual RTTITypeBase* getRTTI() const override;
  335. };
  336. /** @} */
  337. }