comparison src/modules/image/devil/CompressedData.cpp @ 1420:2a5ef265e040 image-CompressedData

Added support for DXT1/3/5 and BC5/7 compressed textures via love.image.CompressedData. Some internals are still iffy. - added a new CompressedData type to love.image - added love.image.isCompressed(file or data) - love.graphics.newImage automatically tests for compression when loading a file - added love.graphics.isSupported strings for DXT, BC5, and BC7 Compressed textures meant to be used on the GPU offer huge performance gains when - loading the texture from a file - loading the texture from RAM to VRAM - loading mipmaps - rendering
author Alex Szpakowski <slime73@gmail.com>
date Thu, 04 Apr 2013 19:09:18 -0300
parents
children aa603d1b03bc
comparison
equal deleted inserted replaced
1419:edd946442f86 1420:2a5ef265e040
1 /**
2 * Copyright (c) 2006-2013 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21 #include "CompressedData.h"
22
23 namespace love
24 {
25 namespace image
26 {
27 namespace devil
28 {
29
30 CompressedData::CompressedData(Data *data)
31 {
32 load(data);
33 }
34
35 CompressedData::CompressedData(love::filesystem::File *file)
36 {
37 Data *data = file->read();
38 try
39 {
40 load(data);
41 }
42 catch (love::Exception &)
43 {
44 data->release();
45 throw;
46 }
47 }
48
49 CompressedData::~CompressedData()
50 {
51
52 }
53
54 bool CompressedData::convertFormat(dds::Format ddsformat)
55 {
56 switch (ddsformat)
57 {
58 case dds::FORMAT_DXT1:
59 type = TYPE_DXT1;
60 break;
61 case dds::FORMAT_DXT3:
62 type = TYPE_DXT3;
63 break;
64 case dds::FORMAT_DXT5:
65 type = TYPE_DXT5;
66 break;
67 case dds::FORMAT_BC5s:
68 type = TYPE_BC5s;
69 break;
70 case dds::FORMAT_BC5u:
71 type = TYPE_BC5u;
72 break;
73 case dds::FORMAT_BC7:
74 type = TYPE_BC7;
75 break;
76 case dds::FORMAT_BC7srgb:
77 type = TYPE_BC7srgb;
78 break;
79 default:
80 return false;
81 }
82
83 return true;
84 }
85
86 void CompressedData::load(Data *data)
87 {
88 if (!dds::Parser::isDDS(data->getData(), data->getSize()))
89 throw love::Exception("Could not decode compressed data (not a DDS file?)");
90
91 try
92 {
93 dds::Parser parser(data->getData(), data->getSize());
94
95 dds::Format format = parser.getFormat();
96
97 if (format == dds::FORMAT_UNKNOWN || !convertFormat(format))
98 throw love::Exception("Could not parse compressed data: Unsupported format.");
99
100 if (parser.getNumMipmaps() == 0)
101 throw love::Exception("Could not parse compressed data: No readable texture data.");
102
103 for (size_t i = 0; i < parser.getNumMipmaps(); i++)
104 {
105 const dds::Parser::Image *img = parser.getImageData(i);
106
107 MipmapInfo mip;
108
109 mip.width = img->width;
110 mip.height = img->height;
111 mip.size = img->dataSize;
112
113 mip.data.resize(mip.size);
114 memcpy(&mip.data[0], img->data, mip.size);
115
116 dataMipmapInfo.push_back(mip);
117 }
118
119 }
120 catch (std::exception &e)
121 {
122 throw love::Exception(e.what());
123 }
124 }
125
126 bool CompressedData::isCompressed(const Data *data)
127 {
128 return dds::Parser::isDDS(data->getData(), data->getSize());
129 }
130
131 } // devil
132 } // image
133 } // love