BsPixelData.h 12 KB

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