BsPixelData.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 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, signed normalized. */
  84. PF_R8S BS_SCRIPT_EXPORT(n:R8S) = 38,
  85. /** 8-bit 2-channel pixel format, signed normalized. */
  86. PF_RG8S BS_SCRIPT_EXPORT(n:RG8S) = 39,
  87. /** 8-bit 4-channel pixel format, signed normalized. */
  88. PF_RGBA8S BS_SCRIPT_EXPORT(n:RGBA8S) = 40,
  89. /** 16-bit 1-channel pixel format, signed integer. */
  90. PF_R16I BS_SCRIPT_EXPORT(n:R16I) = 41,
  91. /** 16-bit 2-channel pixel format, signed integer. */
  92. PF_RG16I BS_SCRIPT_EXPORT(n:RG16I) = 42,
  93. /** 16-bit 4-channel pixel format, signed integer. */
  94. PF_RGBA16I BS_SCRIPT_EXPORT(n:RGBA16I) = 43,
  95. /** 32-bit 1-channel pixel format, signed integer. */
  96. PF_R32I BS_SCRIPT_EXPORT(n:R32I) = 44,
  97. /** 32-bit 2-channel pixel format, signed integer. */
  98. PF_RG32I BS_SCRIPT_EXPORT(n:RG32I) = 45,
  99. /** 32-bit 3-channel pixel format, signed integer. */
  100. PF_RGB32I BS_SCRIPT_EXPORT(n:RGB32I) = 46,
  101. /** 32-bit 4-channel pixel format, signed integer. */
  102. PF_RGBA32I BS_SCRIPT_EXPORT(n:RGBA32I) = 47,
  103. /** 16-bit 1-channel pixel format, signed normalized. */
  104. PF_R16S BS_SCRIPT_EXPORT(n:R16S) = 48,
  105. /** 16-bit 2-channel pixel format, signed normalized. */
  106. PF_RG16S BS_SCRIPT_EXPORT(n:RG16S) = 49,
  107. /** 16-bit 4-channel pixel format, signed normalized. */
  108. PF_RGBA16S BS_SCRIPT_EXPORT(n:RGBA16S) = 50,
  109. /** 16-bit 1-channel pixel format, unsigned normalized. */
  110. PF_R16 BS_SCRIPT_EXPORT(n:R16) = 51,
  111. /** 16-bit 2-channel pixel format, unsigned normalized. */
  112. PF_RG16 BS_SCRIPT_EXPORT(n:RG16) = 52,
  113. /** 16-bit 3-channel pixel format, unsigned normalized. */
  114. PF_RGB16 BS_SCRIPT_EXPORT(n:RGB16) = 53,
  115. /** 16-bit 4-channel pixel format, unsigned normalized. */
  116. PF_RGBA16 BS_SCRIPT_EXPORT(n:RGBA16) = 54,
  117. /** Number of pixel formats currently defined. */
  118. PF_COUNT BS_SCRIPT_EXPORT(ex:true)
  119. };
  120. /** Flags defining some properties of pixel formats. */
  121. enum PixelFormatFlags {
  122. /** This format has an alpha channel. */
  123. PFF_HASALPHA = 0x1,
  124. /**
  125. * This format is compressed. This invalidates the values in elemBytes, elemBits and the bit counts as these might
  126. * not be fixed in a compressed format.
  127. */
  128. PFF_COMPRESSED = 0x2,
  129. /** This is a floating point format. */
  130. PFF_FLOAT = 0x4,
  131. /** This is a depth format (for depth textures). */
  132. PFF_DEPTH = 0x8,
  133. /** This format stores data internally as integers. */
  134. PFF_INTEGER = 0x10,
  135. /** Format contains signed data. Absence of this flag implies unsigned data. */
  136. PFF_SIGNED = 0x20,
  137. /** Format contains normalized data. This will be [0, 1] for unsigned, and [-1,1] for signed formats. */
  138. PFF_NORMALIZED = 0x40
  139. };
  140. /** Types used for individual components of a pixel. */
  141. enum PixelComponentType
  142. {
  143. PCT_BYTE = 0, /**< 8-bit integer per component */
  144. PCT_SHORT = 1, /**< 16-bit integer per component. */
  145. PCT_INT = 2, /**< 32-bit integer per component. */
  146. PCT_FLOAT16 = 3, /**< 16 bit float per component */
  147. PCT_FLOAT32 = 4, /**< 32 bit float per component */
  148. PCT_PACKED_R11G11B10 = 5, /**< 11 bits for first two components, 10 for third component. */
  149. PCT_PACKED_R10G10B10A2 = 6, /**< 10 bits for first three components, 2 bits for last component */
  150. PCT_COUNT /**< Number of pixel types */
  151. };
  152. /** Determines how are texture pixels filtered during sampling. */
  153. enum TextureFilter
  154. {
  155. /** Pixel nearest to the sampled location is chosen. */
  156. TF_NEAREST,
  157. /** Four pixels nearest to the sampled location are interpolated to yield the sampled color. */
  158. TF_BILINEAR
  159. };
  160. /** A list of cubemap faces. */
  161. enum CubemapFace
  162. {
  163. CF_PositiveX,
  164. CF_NegativeX,
  165. CF_PositiveY,
  166. CF_NegativeY,
  167. CF_PositiveZ,
  168. CF_NegativeZ
  169. };
  170. /**
  171. * A buffer describing a volume (3D), image (2D) or line (1D) of pixels in memory. Pixels are stored as a succession
  172. * of "depth" slices, each containing "height" rows of "width" pixels.
  173. *
  174. * @note
  175. * If using the constructor instead of create() you must call GpuResourceData::allocateInternalBuffer or set the buffer
  176. * in some other way before reading/writing from this object, as by the default there is no buffer allocated.
  177. *
  178. * @see GpuResourceData
  179. */
  180. class BS_CORE_EXPORT BS_SCRIPT_EXPORT() PixelData : public GpuResourceData
  181. {
  182. public:
  183. PixelData();
  184. ~PixelData() {}
  185. /**
  186. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  187. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  188. */
  189. PixelData(const PixelVolume& extents, PixelFormat pixelFormat);
  190. /**
  191. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  192. * height and depth, where each pixel is of the specified pixel format.
  193. */
  194. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat);
  195. PixelData(const PixelData& copy);
  196. PixelData& operator=(const PixelData& rhs);
  197. /**
  198. * Returns the number of pixels that offsets one row from another. This can be "width", but doesn't have to be as
  199. * some buffers require padding.
  200. */
  201. BS_SCRIPT_EXPORT(n:RawRowPitch,pr:getter)
  202. UINT32 getRowPitch() const { return mRowPitch; }
  203. /**
  204. * Returns the number of pixels that offsets one depth slice from another. This can be "width * height", but
  205. * doesn't have to be as some buffers require padding.
  206. */
  207. BS_SCRIPT_EXPORT(n:RawSlicePitch,pr:getter)
  208. UINT32 getSlicePitch() const { return mSlicePitch; }
  209. /**
  210. * Sets the pitch (in pixels) that determines offset between rows of the pixel buffer. Call this before allocating
  211. * the buffer.
  212. */
  213. void setRowPitch(UINT32 rowPitch) { mRowPitch = rowPitch; }
  214. /**
  215. * Sets the pitch (in pixels) that determines offset between depth slices of the pixel buffer. Call this before
  216. * allocating the buffer.
  217. */
  218. void setSlicePitch(UINT32 slicePitch) { mSlicePitch = slicePitch; }
  219. /**
  220. * Returns the number of extra pixels in a row (non-zero only if rows are not consecutive (row pitch is larger
  221. * than width)).
  222. */
  223. UINT32 getRowSkip() const { return mRowPitch - getWidth(); }
  224. /**
  225. * Returns the number of extra pixels in a depth slice (non-zero only if slices aren't consecutive (slice pitch is
  226. * larger than width*height).
  227. */
  228. UINT32 getSliceSkip() const { return mSlicePitch - (getHeight() * mRowPitch); }
  229. /** Returns the pixel format used by the internal buffer for storing the pixels. */
  230. BS_SCRIPT_EXPORT(n:Format,pr:getter)
  231. PixelFormat getFormat() const { return mFormat; }
  232. /** Returns width of the buffer in pixels. */
  233. UINT32 getWidth() const { return mExtents.getWidth(); }
  234. /** Returns height of the buffer in pixels. */
  235. UINT32 getHeight() const { return mExtents.getHeight(); }
  236. /** Returns depth of the buffer in pixels. */
  237. UINT32 getDepth() const { return mExtents.getDepth(); }
  238. /**
  239. * Returns left-most start of the pixel volume. This value is not used internally in any way. It is just passed
  240. * through from the constructor.
  241. */
  242. UINT32 getLeft() const { return mExtents.left; }
  243. /**
  244. * Returns right-most end of the pixel volume. This value is not used internally in any way. It is just passed
  245. * through from the constructor.
  246. */
  247. UINT32 getRight() const { return mExtents.right; }
  248. /**
  249. * Returns top-most start of the pixel volume. This value is not used internally in any way. It is just passed
  250. * through from the constructor.
  251. */
  252. UINT32 getTop() const { return mExtents.top; }
  253. /**
  254. * Returns bottom-most end of the pixel volume. This value is not used internally in any way. It is just passed
  255. * through from the constructor.
  256. */
  257. UINT32 getBottom() const { return mExtents.bottom; }
  258. /**
  259. * Returns front-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 getFront() const { return mExtents.front; }
  263. /**
  264. * Returns back-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 getBack() const { return mExtents.back; }
  268. /** Returns extents of the pixel volume this object is capable of holding. */
  269. BS_SCRIPT_EXPORT(n:Extents,pr:getter)
  270. PixelVolume getExtents() const { return mExtents; }
  271. /**
  272. * Return whether this buffer is laid out consecutive in memory (meaning the pitches are equal to the dimensions).
  273. */
  274. BS_SCRIPT_EXPORT(n:RawIsConsecutive,pr:getter)
  275. bool isConsecutive() const
  276. {
  277. return mRowPitch == getWidth() && mSlicePitch == getWidth()*getHeight();
  278. }
  279. /** Return the size (in bytes) this image would take if it was laid out consecutive in memory. */
  280. UINT32 getConsecutiveSize() const;
  281. /** Return the size (in bytes) of the buffer this image requires. */
  282. BS_SCRIPT_EXPORT(n:RawSize,pr:getter)
  283. UINT32 getSize() const;
  284. /**
  285. * Returns pixel data containing a sub-volume of this object. Returned data will not have its own buffer, but will
  286. * instead point to this one. It is up to the caller to ensure this object outlives any sub-volume objects.
  287. */
  288. PixelData getSubVolume(const PixelVolume& volume) const;
  289. /**
  290. * Samples a color at the specified coordinates using a specific filter.
  291. *
  292. * @param[in] coords Coordinates to sample the color at. They start at top left corner (0, 0), and are in range
  293. * [0, 1].
  294. * @param[in] filter Filtering mode to use when sampling the color.
  295. * @return Sampled color.
  296. */
  297. Color sampleColorAt(const Vector2& coords, TextureFilter filter = TF_BILINEAR) const;
  298. /** Returns pixel color at the specified coordinates. */
  299. Color getColorAt(UINT32 x, UINT32 y, UINT32 z = 0) const;
  300. /** Sets the pixel color at the specified coordinates. */
  301. void setColorAt(const Color& color, UINT32 x, UINT32 y, UINT32 z = 0);
  302. /**
  303. * Converts all the internal data into an array of colors. Array is mapped as such:
  304. * arrayIdx = x + y * width + z * width * height.
  305. */
  306. Vector<Color> getColors() const;
  307. /**
  308. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  309. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  310. */
  311. void setColors(const Vector<Color>& colors);
  312. /**
  313. * Initializes the internal buffer with the provided set of colors. The array should be of
  314. * width * height * depth size and mapped as such: arrayIdx = x + y * width + z * width * height.
  315. */
  316. void setColors(Color* colors, UINT32 numElements);
  317. /**
  318. * Interprets pixel data as depth information as retrieved from the GPU's depth buffer. Converts the device specific
  319. * depth value to range [0, 1] and returns it.
  320. */
  321. float getDepthAt(UINT32 x, UINT32 y, UINT32 z = 0) const;
  322. /**
  323. * Converts all the internal data into an array of floats as if each individual pixel is retrieved with
  324. * getDepthAt(). Array is mapped as such: arrayIdx = x + y * width + z * width * height.
  325. */
  326. Vector<float> getDepths() const;
  327. /**
  328. * Constructs a new object with an internal buffer capable of holding "extents" volume of pixels, where each pixel
  329. * is of the specified pixel format. Extent offsets are also stored, but are not used internally.
  330. */
  331. static SPtr<PixelData> create(const PixelVolume& extents, PixelFormat pixelFormat);
  332. /**
  333. * Constructs a new object with an internal buffer capable of holding volume of pixels described by provided width,
  334. * height and depth, where each pixel is of the specified pixel format.
  335. */
  336. static SPtr<PixelData> create(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat);
  337. private:
  338. /**
  339. * Initializes the internal buffer with the provided set of colors. The array should be of width * height * depth
  340. * size and mapped as such: arrayIdx = x + y * width + z * width * height.
  341. *
  342. * @note A generic method that is reused in other more specific setColors() calls.
  343. */
  344. template<class T>
  345. void setColorsInternal(const T& colors, UINT32 numElements);
  346. /** Returns the needed size of the internal buffer, in bytes. */
  347. UINT32 getInternalBufferSize() const override;
  348. private:
  349. PixelVolume mExtents;
  350. PixelFormat mFormat;
  351. UINT32 mRowPitch;
  352. UINT32 mSlicePitch;
  353. /************************************************************************/
  354. /* SERIALIZATION */
  355. /************************************************************************/
  356. public:
  357. friend class PixelDataRTTI;
  358. static RTTITypeBase* getRTTIStatic();
  359. RTTITypeBase* getRTTI() const override;
  360. };
  361. /** @} */
  362. }