CmPixelData.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmPixelVolume.h"
  4. #include "CmGpuResourceData.h"
  5. #include "CmIReflectable.h"
  6. namespace CamelotFramework
  7. {
  8. /** The pixel format used for images, textures, and render surfaces */
  9. enum PixelFormat
  10. {
  11. /// Unknown pixel format.
  12. PF_UNKNOWN = 0,
  13. /// 8-bit pixel format, all bits red
  14. PF_R8 = 1,
  15. /// 2 byte pixel format, 1 byte red, 1 byte green
  16. PF_R8G8 = 2,
  17. /// 24-bit pixel format, 8 bits for red, green and blue.
  18. PF_R8G8B8 = 3,
  19. /// 24-bit pixel format, 8 bits for blue, green and red.
  20. PF_B8G8R8 = 4,
  21. /// 32-bit pixel format, 8 bits for alpha, red, green and blue.
  22. PF_A8R8G8B8 = 5,
  23. /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
  24. PF_A8B8G8R8 = 6,
  25. /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
  26. PF_B8G8R8A8 = 7,
  27. /// 32-bit pixel format, 8 bits for red, green, blue and alpha.
  28. PF_R8G8B8A8 = 8,
  29. /// 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue
  30. /// like PF_A8R8G8B8, but alpha will get discarded
  31. PF_X8R8G8B8 = 9,
  32. /// 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red
  33. /// like PF_A8B8G8R8, but alpha will get discarded
  34. PF_X8B8G8R8 = 10,
  35. /// 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue
  36. /// like PF_R8G8B8A8, but alpha will get discarded
  37. PF_R8G8B8X8 = 11,
  38. /// 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red
  39. /// like PF_B8G8R8A8, but alpha will get discarded
  40. PF_B8G8R8X8 = 12,
  41. /// 3 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue
  42. PF_BYTE_RGB = PF_R8G8B8,
  43. /// 3 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red
  44. PF_BYTE_BGR = PF_B8G8R8,
  45. /// 4 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red and one byte for alpha
  46. PF_BYTE_BGRA = PF_B8G8R8A8,
  47. /// 4 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue, and one byte for alpha
  48. PF_BYTE_RGBA = PF_R8G8B8A8,
  49. /// DDS (DirectDraw Surface) DXT1 format
  50. PF_DXT1 = 13,
  51. /// DDS (DirectDraw Surface) DXT2 format
  52. PF_DXT2 = 14,
  53. /// DDS (DirectDraw Surface) DXT3 format
  54. PF_DXT3 = 15,
  55. /// DDS (DirectDraw Surface) DXT4 format
  56. PF_DXT4 = 16,
  57. /// DDS (DirectDraw Surface) DXT5 format
  58. PF_DXT5 = 17,
  59. // 16-bit pixel format, 16 bits (float) for red
  60. PF_FLOAT16_R = 18,
  61. // 32-bit, 2-channel s10e5 floating point pixel format, 16-bit red, 16-bit green
  62. PF_FLOAT16_RG = 19,
  63. // 48-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue
  64. PF_FLOAT16_RGB = 20,
  65. // 64-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue, 16 bits (float) for alpha
  66. PF_FLOAT16_RGBA = 21,
  67. // 32-bit pixel format, 32 bits (float) for red
  68. PF_FLOAT32_R = 22,
  69. // 64-bit, 2-channel floating point pixel format, 32-bit red, 32-bit green
  70. PF_FLOAT32_RG = 23,
  71. // 96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue
  72. PF_FLOAT32_RGB = 24,
  73. // 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha
  74. PF_FLOAT32_RGBA = 25,
  75. // Depth stencil, 32bit depth, 8bit stencil + 24 unused
  76. PF_D32_S8X24 = 26,
  77. // Depth stencil, 24bit depth + 8bit stencil
  78. PF_D24S8 = 27,
  79. // Depth, 32bits
  80. PF_D32 = 28,
  81. // Depth, 16bits
  82. PF_D16 = 29,
  83. // Number of pixel formats currently defined
  84. PF_COUNT = 30
  85. };
  86. typedef Vector<PixelFormat>::type PixelFormatList;
  87. /**
  88. * Flags defining some on/off properties of pixel formats
  89. */
  90. enum PixelFormatFlags {
  91. // This format has an alpha channel
  92. PFF_HASALPHA = 0x00000001,
  93. // This format is compressed. This invalidates the values in elemBytes,
  94. // elemBits and the bit counts as these might not be fixed in a compressed format.
  95. PFF_COMPRESSED = 0x00000002,
  96. // This is a floating point format
  97. PFF_FLOAT = 0x00000004,
  98. // This is a depth format (for depth textures)
  99. PFF_DEPTH = 0x00000008,
  100. // Format is in native endian. Generally true for the 16, 24 and 32 bits
  101. // formats which can be represented as machine integers.
  102. PFF_NATIVEENDIAN = 0x00000010
  103. };
  104. /** Pixel component format */
  105. enum PixelComponentType
  106. {
  107. PCT_BYTE = 0, /// Byte per component (8 bit fixed 0.0..1.0)
  108. PCT_SHORT = 1, /// Short per component (16 bit fixed 0.0..1.0))
  109. PCT_FLOAT16 = 2, /// 16 bit float per component
  110. PCT_FLOAT32 = 3, /// 32 bit float per component
  111. PCT_COUNT = 4 /// Number of pixel types
  112. };
  113. /** A primitive describing a volume (3D), image (2D) or line (1D) of pixels in memory.
  114. In case of a rectangle, depth must be 1.
  115. Pixels are stored as a succession of "depth" slices, each containing "height" rows of
  116. "width" pixels.
  117. */
  118. class CM_EXPORT PixelData : public GpuResourceData
  119. {
  120. public:
  121. /// Parameter constructor for setting the members manually
  122. PixelData() {}
  123. ~PixelData() {}
  124. /** Constructor providing extents in the form of a Box object. This constructor
  125. assumes the pixel data is laid out consecutively in memory. (this
  126. means row after row, slice after slice, with no space in between)
  127. @param extents Extents of the region defined by data
  128. @param pixelFormat Format of this buffer
  129. @param pixelData Pointer to the actual data
  130. */
  131. PixelData(const PixelVolume &extents, PixelFormat pixelFormat)
  132. :mExtents(extents), mFormat(pixelFormat)
  133. {
  134. setConsecutive();
  135. }
  136. /** Constructor providing width, height and depth. This constructor
  137. assumes the pixel data is laid out consecutively in memory. (this
  138. means row after row, slice after slice, with no space in between)
  139. @param width Width of the region
  140. @param height Height of the region
  141. @param depth Depth of the region
  142. @param pixelFormat Format of this buffer
  143. @param pixelData Pointer to the actual data
  144. */
  145. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat)
  146. : mExtents(0, 0, 0, width, height, depth), mFormat(pixelFormat)
  147. {
  148. setConsecutive();
  149. }
  150. PixelData(const PixelData& copy);
  151. UINT32 getRowPitch() const { return mRowPitch; }
  152. UINT32 getSlicePitch() const { return mSlicePitch; }
  153. void setRowPitch(UINT32 rowPitch) { mRowPitch = rowPitch; }
  154. void setSlicePitch(UINT32 slicePitch) { mSlicePitch = slicePitch; }
  155. /** Get the number of elements between one past the rightmost pixel of
  156. one row and the leftmost pixel of the next row. (IE this is zero if rows
  157. are consecutive).
  158. */
  159. UINT32 getRowSkip() const { return mRowPitch - getWidth(); }
  160. /** Get the number of elements between one past the right bottom pixel of
  161. one slice and the left top pixel of the next slice. (IE this is zero if slices
  162. are consecutive).
  163. */
  164. UINT32 getSliceSkip() const { return mSlicePitch - (getHeight() * mRowPitch); }
  165. PixelFormat getFormat() const { return mFormat; }
  166. UINT32 getWidth() const { return mExtents.getWidth(); }
  167. UINT32 getHeight() const { return mExtents.getHeight(); }
  168. UINT32 getDepth() const { return mExtents.getDepth(); }
  169. UINT32 getLeft() const { return mExtents.left; }
  170. UINT32 getRight() const { return mExtents.right; }
  171. UINT32 getTop() const { return mExtents.top; }
  172. UINT32 getBottom() const { return mExtents.bottom; }
  173. UINT32 getFront() const { return mExtents.front; }
  174. UINT32 getBack() const { return mExtents.back; }
  175. PixelVolume getExtents() const { return mExtents; }
  176. /** Return whether this buffer is laid out consecutive in memory (ie the pitches
  177. are equal to the dimensions)
  178. */
  179. bool isConsecutive() const
  180. {
  181. return mRowPitch == getWidth() && mSlicePitch == getWidth()*getHeight();
  182. }
  183. /** Return the size (in bytes) this image would take if it was
  184. laid out consecutive in memory
  185. */
  186. UINT32 getConsecutiveSize() const;
  187. /** Return a subvolume of this PixelBox.
  188. @param def Defines the bounds of the subregion to return
  189. @returns A pixel box describing the region and the data in it
  190. @remarks This function does not copy any data, it just returns
  191. a PixelBox object with a data pointer pointing somewhere inside
  192. the data of object.
  193. @throws Exception(ERR_INVALIDPARAMS) if def is not fully contained
  194. */
  195. PixelData getSubVolume(const PixelVolume &def) const;
  196. /**
  197. * Get colour value from a certain location in the PixelBox. The z coordinate
  198. * is only valid for cubemaps and volume textures. This uses the first (largest)
  199. * mipmap.
  200. */
  201. Color getColorAt(UINT32 x, UINT32 y, UINT32 z = 0);
  202. /**
  203. * Set colour value at a certain location in the PixelBox. The z coordinate
  204. * is only valid for cubemaps and volume textures. This uses the first (largest)
  205. * mipmap.
  206. */
  207. void setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z = 0);
  208. private:
  209. PixelVolume mExtents;
  210. /// The pixel format
  211. PixelFormat mFormat;
  212. /** Number of elements between the leftmost pixel of one row and the left
  213. pixel of the next. This value must always be equal to getWidth() (consecutive)
  214. for compressed formats.
  215. */
  216. UINT32 mRowPitch;
  217. /** Number of elements between the top left pixel of one (depth) slice and
  218. the top left pixel of the next. This can be a negative value. Must be a multiple of
  219. rowPitch. This value must always be equal to getWidth()*getHeight() (consecutive)
  220. for compressed formats.
  221. */
  222. UINT32 mSlicePitch;
  223. /** Set the rowPitch and slicePitch so that the buffer is laid out consecutive
  224. in memory.
  225. */
  226. void setConsecutive()
  227. {
  228. mRowPitch = getWidth();
  229. mSlicePitch = getWidth()*getHeight();
  230. }
  231. UINT32 getInternalBufferSize();
  232. /************************************************************************/
  233. /* SERIALIZATION */
  234. /************************************************************************/
  235. public:
  236. friend class PixelDataRTTI;
  237. static RTTITypeBase* getRTTIStatic();
  238. virtual RTTITypeBase* getRTTI() const;
  239. };
  240. }