DXTCompressor.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef __DXT_COMPRESSOR_H__
  2. #define __DXT_COMPRESSOR_H__
  3. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  4. // Includes
  5. #include <GL/glew.h>
  6. #include <GL/glut.h>
  7. #include <Cg/cg.h>
  8. #include <Cg/cgGL.h>
  9. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  10. // Forward decls.
  11. class FramebufferObject;
  12. class Renderbuffer;
  13. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  14. enum CompressionType
  15. {
  16. DXT1,
  17. DXT5_YCOCG,
  18. };
  19. /* ------------------------------------------------------------------------------------------------------------------------------------ */
  20. class DXTCompressor
  21. {
  22. public:
  23. DXTCompressor();
  24. public:
  25. // These need to be called before using the compressor, and shutdown after using the compressor
  26. static bool Initialize();
  27. static void Shutdown();
  28. // Compresses input data into the desired compressed format.
  29. // The input data is assumed to be 32-bit RGBA (1 byte per channel).
  30. // outputData should be allocated to at least GetBufferSize()
  31. // This will return true if the compression is successful.
  32. static bool CompressImageData( CompressionType compressionType, const void *inputRGBA, int inputWidth, int inputHeight, void *outputData, bool bDisplayResults, bool sourceFormatIsBGRA );
  33. static int GetBufferSize(CompressionType compressionType, int inputWidth, int inputHeight );
  34. // Saves the compressed data into a Microsoft DDS file.
  35. static int GetDDSHeaderSize(void);
  36. static void WriteDDSHeader( CompressionType compressionType, int width, int height, int compresedDataLength, void *outputData );
  37. static void WriteDDSMemoryFile( CompressionType compressionType, int width, int height, const void* pCompressedData, int compresedDataLength, void **outputData, int *outputLength );
  38. // Prints out the accumulated performance of the compressor
  39. static void PrintPerformanceLog();
  40. private:
  41. void SetShaderConstants();
  42. void CompressInternal( bool sourceFormatIsBGRA);
  43. void DoCompression( void* ppOutputData, bool sourceFormatIsBGRA );
  44. static bool InitOpenGL();
  45. static bool InitCG();
  46. static void cgErrorCallback();
  47. static bool IsInitialized();
  48. static void DisplayTexture( GLuint textureID, int texW, int texH );
  49. static FramebufferObject* RequestFBO( CompressionType compressionType, int width, int height );
  50. static void DeallocateBuffers();
  51. private:
  52. // Image related data
  53. int m_imageWidth;
  54. int m_imageHeight;
  55. CompressionType m_compressionType;
  56. GLuint m_imageTexId;
  57. FramebufferObject* m_pCompressFbo;
  58. GLuint m_compressFboTex;
  59. // CG related data
  60. static int m_initRefCount;
  61. static CGcontext m_cgContext;
  62. static CGprofile m_cgVProfile, m_cgFProfile;
  63. static CGprogram m_compressVProg, m_compressDXT1RGBAFProg, m_compressDXT5RGBAFProg, m_compressDXT1BGRAFProg, m_compressDXT5BGRAFProg;
  64. // Used to collect stats
  65. static int m_numIterations;
  66. static int m_currentImageWidth, m_currentImageHeight;
  67. static float m_lastCompressionTime;
  68. static float m_accumulatedTime;
  69. static float m_timeRunningCompressionShader;
  70. static float m_timeCopyingPixelDataToCPU;
  71. };
  72. #endif // __DXT_COMPRESSOR_H__
粤ICP备19079148号