1 /*
2 Real-time DXT1 & YCoCg DXT5 compression (Cg 2.0)
3 Copyright (c) NVIDIA Corporation.
4 Written by: Ignacio Castano <icastano@nvidia.com>
5 
6 Thanks to JMP van Waveren, Simon Green, Eric Werness, Simon Brown
7 
8 Permission is hereby granted, free of charge, to any person
9 obtaining a copy of this software and associated documentation
10 files (the "Software"), to deal in the Software without
11 restriction, including without limitation the rights to use,
12 copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following
15 conditions:
16 
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 OTHER DEALINGS IN THE SOFTWARE.
28 */
29 
30 const char* pDXTCompressorShaderSource =
31 "	\n"
32 "// vertex program	\n"
33 "void compress_vp(float4 pos : POSITION,	\n"
34 "                      float2 texcoord : TEXCOORD0,	\n"
35 "                      out float4 hpos : POSITION,	\n"
36 "                      out float2 o_texcoord : TEXCOORD0	\n"
37 "                      )	\n"
38 "{	\n"
39 "    o_texcoord = texcoord;	\n"
40 "    hpos = pos;	\n"
41 "}	\n"
42 "	\n"
43 "typedef unsigned int uint;	\n"
44 "typedef unsigned int2 uint2;	\n"
45 "typedef unsigned int3 uint3;	\n"
46 "typedef unsigned int4 uint4;	\n"
47 "	\n"
48 "const float offset = 128.0 / 255.0;	\n"
49 "	\n"
50 "// Use dot product to minimize RMS instead absolute distance like in the CPU compressor.	\n"
51 "float colorDistance(float3 c0, float3 c1)	\n"
52 "{	\n"
53 "    return dot(c0-c1, c0-c1);	\n"
54 "}	\n"
55 "float colorDistance(float2 c0, float2 c1)	\n"
56 "{	\n"
57 "    return dot(c0-c1, c0-c1);	\n"
58 "}	\n"
59 "	\n"
60 "	\n"
61 "void ExtractColorBlockRGB(out float3 col[16], sampler2D image, float2 texcoord, float2 imageSize)	\n"
62 "{	\n"
63 "    // use TXF instruction (integer coordinates with offset)	\n"
64 "    // note offsets must be constant	\n"
65 "    //int4 base = int4(wpos*4-2, 0, 0);	\n"
66 "    int4 base = int4(texcoord * imageSize - 1.5, 0, 0);	\n"
67 "    col[0] = tex2Dfetch(image, base, int2(0, 0)).rgb;	\n"
68 "    col[1] = tex2Dfetch(image, base, int2(1, 0)).rgb;	\n"
69 "    col[2] = tex2Dfetch(image, base, int2(2, 0)).rgb;	\n"
70 "    col[3] = tex2Dfetch(image, base, int2(3, 0)).rgb;	\n"
71 "    col[4] = tex2Dfetch(image, base, int2(0, 1)).rgb;	\n"
72 "    col[5] = tex2Dfetch(image, base, int2(1, 1)).rgb;	\n"
73 "    col[6] = tex2Dfetch(image, base, int2(2, 1)).rgb;	\n"
74 "    col[7] = tex2Dfetch(image, base, int2(3, 1)).rgb;	\n"
75 "    col[8] = tex2Dfetch(image, base, int2(0, 2)).rgb;	\n"
76 "    col[9] = tex2Dfetch(image, base, int2(1, 2)).rgb;	\n"
77 "    col[10] = tex2Dfetch(image, base, int2(2, 2)).rgb;	\n"
78 "    col[11] = tex2Dfetch(image, base, int2(3, 2)).rgb;	\n"
79 "    col[12] = tex2Dfetch(image, base, int2(0, 3)).rgb;	\n"
80 "    col[13] = tex2Dfetch(image, base, int2(1, 3)).rgb;	\n"
81 "    col[14] = tex2Dfetch(image, base, int2(2, 3)).rgb;	\n"
82 "    col[15] = tex2Dfetch(image, base, int2(3, 3)).rgb;	\n"
83 "}	\n"
84 "	\n"
85 "void ExtractColorBlockBGR(out float3 col[16], sampler2D image, float2 texcoord, float2 imageSize)	\n"
86 "{	\n"
87 "    // use TXF instruction (integer coordinates with offset)	\n"
88 "    // note offsets must be constant	\n"
89 "    //int4 base = int4(wpos*4-2, 0, 0);	\n"
90 "    int4 base = int4(texcoord * imageSize - 1.5, 0, 0);	\n"
91 "    col[0] = tex2Dfetch(image, base, int2(0, 0)).bgr;	\n"
92 "    col[1] = tex2Dfetch(image, base, int2(1, 0)).bgr;	\n"
93 "    col[2] = tex2Dfetch(image, base, int2(2, 0)).bgr;	\n"
94 "    col[3] = tex2Dfetch(image, base, int2(3, 0)).bgr;	\n"
95 "    col[4] = tex2Dfetch(image, base, int2(0, 1)).bgr;	\n"
96 "    col[5] = tex2Dfetch(image, base, int2(1, 1)).bgr;	\n"
97 "    col[6] = tex2Dfetch(image, base, int2(2, 1)).bgr;	\n"
98 "    col[7] = tex2Dfetch(image, base, int2(3, 1)).bgr;	\n"
99 "    col[8] = tex2Dfetch(image, base, int2(0, 2)).bgr;	\n"
100 "    col[9] = tex2Dfetch(image, base, int2(1, 2)).bgr;	\n"
101 "    col[10] = tex2Dfetch(image, base, int2(2, 2)).bgr;	\n"
102 "    col[11] = tex2Dfetch(image, base, int2(3, 2)).bgr;	\n"
103 "    col[12] = tex2Dfetch(image, base, int2(0, 3)).bgr;	\n"
104 "    col[13] = tex2Dfetch(image, base, int2(1, 3)).bgr;	\n"
105 "    col[14] = tex2Dfetch(image, base, int2(2, 3)).bgr;	\n"
106 "    col[15] = tex2Dfetch(image, base, int2(3, 3)).bgr;	\n"
107 "}	\n"
108 "	\n"
109 "float3 toYCoCg(float3 c)	\n"
110 "{	\n"
111 "    float Y = (c.r + 2 * c.g + c.b) * 0.25;	\n"
112 "    float Co = ( ( 2 * c.r - 2 * c.b      ) * 0.25 + offset );	\n"
113 "    float Cg = ( (    -c.r + 2 * c.g - c.b) * 0.25 + offset );	\n"
114 "	\n"
115 "    return float3(Y, Co, Cg);	\n"
116 "}	\n"
117 "	\n"
118 "void ExtractColorBlockYCoCg_RGB(out float3 col[16], sampler2D image, float2 texcoord, float2 imageSize)	\n"
119 "{	\n"
120 "    // use TXF instruction (integer coordinates with offset)	\n"
121 "    // note offsets must be constant	\n"
122 "    //int4 base = int4(wpos*4-2, 0, 0);	\n"
123 "    int4 base = int4(texcoord * imageSize - 1.5, 0, 0);	\n"
124 "    col[0] = toYCoCg(tex2Dfetch(image, base, int2(0, 0)).rgb);	\n"
125 "    col[1] = toYCoCg(tex2Dfetch(image, base, int2(1, 0)).rgb);	\n"
126 "    col[2] = toYCoCg(tex2Dfetch(image, base, int2(2, 0)).rgb);	\n"
127 "    col[3] = toYCoCg(tex2Dfetch(image, base, int2(3, 0)).rgb);	\n"
128 "    col[4] = toYCoCg(tex2Dfetch(image, base, int2(0, 1)).rgb);	\n"
129 "    col[5] = toYCoCg(tex2Dfetch(image, base, int2(1, 1)).rgb);	\n"
130 "    col[6] = toYCoCg(tex2Dfetch(image, base, int2(2, 1)).rgb);	\n"
131 "    col[7] = toYCoCg(tex2Dfetch(image, base, int2(3, 1)).rgb);	\n"
132 "    col[8] = toYCoCg(tex2Dfetch(image, base, int2(0, 2)).rgb);	\n"
133 "    col[9] = toYCoCg(tex2Dfetch(image, base, int2(1, 2)).rgb);	\n"
134 "    col[10] = toYCoCg(tex2Dfetch(image, base, int2(2, 2)).rgb);	\n"
135 "    col[11] = toYCoCg(tex2Dfetch(image, base, int2(3, 2)).rgb);	\n"
136 "    col[12] = toYCoCg(tex2Dfetch(image, base, int2(0, 3)).rgb);	\n"
137 "    col[13] = toYCoCg(tex2Dfetch(image, base, int2(1, 3)).rgb);	\n"
138 "    col[14] = toYCoCg(tex2Dfetch(image, base, int2(2, 3)).rgb);	\n"
139 "    col[15] = toYCoCg(tex2Dfetch(image, base, int2(3, 3)).rgb);	\n"
140 "}	\n"
141 "	\n"
142 "void ExtractColorBlockYCoCg_BGR(out float3 col[16], sampler2D image, float2 texcoord, float2 imageSize)	\n"
143 "{	\n"
144 "    // use TXF instruction (integer coordinates with offset)	\n"
145 "    // note offsets must be constant	\n"
146 "    //int4 base = int4(wpos*4-2, 0, 0);	\n"
147 "    int4 base = int4(texcoord * imageSize - 1.5, 0, 0);	\n"
148 "    col[0] = toYCoCg(tex2Dfetch(image, base, int2(0, 0)).bgr);	\n"
149 "    col[1] = toYCoCg(tex2Dfetch(image, base, int2(1, 0)).bgr);	\n"
150 "    col[2] = toYCoCg(tex2Dfetch(image, base, int2(2, 0)).bgr);	\n"
151 "    col[3] = toYCoCg(tex2Dfetch(image, base, int2(3, 0)).bgr);	\n"
152 "    col[4] = toYCoCg(tex2Dfetch(image, base, int2(0, 1)).bgr);	\n"
153 "    col[5] = toYCoCg(tex2Dfetch(image, base, int2(1, 1)).bgr);	\n"
154 "    col[6] = toYCoCg(tex2Dfetch(image, base, int2(2, 1)).bgr);	\n"
155 "    col[7] = toYCoCg(tex2Dfetch(image, base, int2(3, 1)).bgr);	\n"
156 "    col[8] = toYCoCg(tex2Dfetch(image, base, int2(0, 2)).bgr);	\n"
157 "    col[9] = toYCoCg(tex2Dfetch(image, base, int2(1, 2)).bgr);	\n"
158 "    col[10] = toYCoCg(tex2Dfetch(image, base, int2(2, 2)).bgr);	\n"
159 "    col[11] = toYCoCg(tex2Dfetch(image, base, int2(3, 2)).bgr);	\n"
160 "    col[12] = toYCoCg(tex2Dfetch(image, base, int2(0, 3)).bgr);	\n"
161 "    col[13] = toYCoCg(tex2Dfetch(image, base, int2(1, 3)).bgr);	\n"
162 "    col[14] = toYCoCg(tex2Dfetch(image, base, int2(2, 3)).bgr);	\n"
163 "    col[15] = toYCoCg(tex2Dfetch(image, base, int2(3, 3)).bgr);	\n"
164 "}	\n"
165 "	\n"
166 "// find minimum and maximum colors based on bounding box in color space	\n"
167 "void FindMinMaxColorsBox(float3 block[16], out float3 mincol, out float3 maxcol)	\n"
168 "{	\n"
169 "    mincol = block[0];	\n"
170 "    maxcol = block[0];	\n"
171 "    	\n"
172 "    for (int i = 1; i < 16; i++) {	\n"
173 "        mincol = min(mincol, block[i]);	\n"
174 "        maxcol = max(maxcol, block[i]);	\n"
175 "    }	\n"
176 "}	\n"
177 "	\n"
178 "void InsetBBox(in out float3 mincol, in out float3 maxcol)	\n"
179 "{	\n"
180 "    float3 inset = (maxcol - mincol) / 16.0 - (8.0 / 255.0) / 16;	\n"
181 "    mincol = saturate(mincol + inset);	\n"
182 "    maxcol = saturate(maxcol - inset);	\n"
183 "}	\n"
184 "void InsetYBBox(in out float mincol, in out float maxcol)	\n"
185 "{	\n"
186 "    float inset = (maxcol - mincol) / 32.0 - (16.0 / 255.0) / 32.0;	\n"
187 "    mincol = saturate(mincol + inset);	\n"
188 "    maxcol = saturate(maxcol - inset);	\n"
189 "}	\n"
190 "void InsetCoCgBBox(in out float2 mincol, in out float2 maxcol)	\n"
191 "{	\n"
192 "    float2 inset = (maxcol - mincol) / 16.0 - (8.0 / 255.0) / 16;	\n"
193 "    mincol = saturate(mincol + inset);	\n"
194 "    maxcol = saturate(maxcol - inset);	\n"
195 "}	\n"
196 "	\n"
197 "void SelectDiagonal(float3 block[16], in out float3 mincol, in out float3 maxcol)	\n"
198 "{	\n"
199 "    float3 center = (mincol + maxcol) * 0.5;	\n"
200 "	\n"
201 "    float2 cov = 0;	\n"
202 "    for (int i = 0; i < 16; i++)	\n"
203 "    {	\n"
204 "        float3 t = block[i] - center;	\n"
205 "        cov.x += t.x * t.z;	\n"
206 "        cov.y += t.y * t.z;	\n"
207 "    }	\n"
208 "	\n"
209 "    if (cov.x < 0) {	\n"
210 "        float temp = maxcol.x;	\n"
211 "        maxcol.x = mincol.x;	\n"
212 "        mincol.x = temp;	\n"
213 "    }	\n"
214 "    if (cov.y < 0) {	\n"
215 "        float temp = maxcol.y;	\n"
216 "        maxcol.y = mincol.y;	\n"
217 "        mincol.y = temp;	\n"
218 "    }	\n"
219 "}	\n"
220 "	\n"
221 "float3 RoundAndExpand(float3 v, out uint w)	\n"
222 "{	\n"
223 "    int3 c = round(v * float3(31, 63, 31));	\n"
224 "    w = (c.r << 11) | (c.g << 5) | c.b;	\n"
225 "	\n"
226 "    c.rb = (c.rb << 3) | (c.rb >> 2);	\n"
227 "    c.g = (c.g << 2) | (c.g >> 4);	\n"
228 "	\n"
229 "    return (float3)c * (1.0 / 255.0);	\n"
230 "}	\n"
231 "	\n"
232 "uint EmitEndPointsDXT1(in out float3 mincol, in out float3 maxcol)	\n"
233 "{	\n"
234 "    uint2 output;	\n"
235 "    maxcol = RoundAndExpand(maxcol, output.x);	\n"
236 "    mincol = RoundAndExpand(mincol, output.y);	\n"
237 "	\n"
238 "    // We have to do this in case we select an alternate diagonal.	\n"
239 "    if (output.x < output.y)	\n"
240 "    {	\n"
241 "        float3 tmp = mincol;	\n"
242 "        mincol = maxcol;	\n"
243 "        maxcol = tmp;	\n"
244 "        return output.y | (output.x << 16);	\n"
245 "    }	\n"
246 "	\n"
247 "    return output.x | (output.y << 16);	\n"
248 "}	\n"
249 "	\n"
250 "uint EmitIndicesDXT1(float3 block[16], float3 mincol, float3 maxcol)	\n"
251 "{	\n"
252 "    const float RGB_RANGE = 3;	\n"
253 "	\n"
254 "    float3 dir = (maxcol - mincol);	\n"
255 "    float3 origin = maxcol + dir / (2.0 * RGB_RANGE);	\n"
256 "    dir /= dot(dir, dir);	\n"
257 "	\n"
258 "    // Compute indices	\n"
259 "    uint indices = 0;	\n"
260 "    for (int i = 0; i < 16; i++)	\n"
261 "    {	\n"
262 "        uint index = saturate(dot(origin - block[i], dir)) * RGB_RANGE;	\n"
263 "        indices |= index << (i * 2);	\n"
264 "    }	\n"
265 "	\n"
266 "    uint i0 = (indices & 0x55555555);	\n"
267 "    uint i1 = (indices & 0xAAAAAAAA) >> 1;	\n"
268 "    indices = ((i0 ^ i1) << 1) | i1;	\n"
269 "	\n"
270 "    // Output indices	\n"
271 "    return indices;	\n"
272 "}	\n"
273 "	\n"
274 "int ScaleYCoCg(float2 minColor, float2 maxColor)	\n"
275 "{	\n"
276 "    float2 m0 = abs(minColor - offset);	\n"
277 "    float2 m1 = abs(maxColor - offset);	\n"
278 "	\n"
279 "    float m = max(max(m0.x, m0.y), max(m1.x, m1.y));	\n"
280 "	\n"
281 "    const float s0 = 64.0 / 255.0;	\n"
282 "    const float s1 = 32.0 / 255.0;	\n"
283 "	\n"
284 "    int scale = 1;	\n"
285 "    if (m < s0) scale = 2;	\n"
286 "    if (m < s1) scale = 4;	\n"
287 "	\n"
288 "    return scale;	\n"
289 "}	\n"
290 "	\n"
291 "void SelectYCoCgDiagonal(const float3 block[16], in out float2 minColor, in out float2 maxColor)	\n"
292 "{	\n"
293 "    float2 mid = (maxColor + minColor) * 0.5;	\n"
294 "	\n"
295 "    float cov = 0;	\n"
296 "    for (int i = 0; i < 16; i++)	\n"
297 "    {	\n"
298 "        float2 t = block[i].yz - mid;	\n"
299 "        cov += t.x * t.y;	\n"
300 "    }	\n"
301 "    if (cov < 0) {	\n"
302 "        float tmp = maxColor.y;	\n"
303 "        maxColor.y = minColor.y;	\n"
304 "        minColor.y = tmp;	\n"
305 "    }	\n"
306 "}	\n"
307 "	\n"
308 "	\n"
309 "uint EmitEndPointsYCoCgDXT5(in out float2 mincol, in out float2 maxcol, int scale)	\n"
310 "{	\n"
311 "    maxcol = (maxcol - offset) * scale + offset;	\n"
312 "    mincol = (mincol - offset) * scale + offset;	\n"
313 "	\n"
314 "    InsetCoCgBBox(mincol, maxcol);	\n"
315 "	\n"
316 "    maxcol = round(maxcol * float2(31, 63));	\n"
317 "    mincol = round(mincol * float2(31, 63));	\n"
318 "	\n"
319 "    int2 imaxcol = maxcol;	\n"
320 "    int2 imincol = mincol;	\n"
321 "	\n"
322 "    uint2 output;	\n"
323 "    output.x = (imaxcol.r << 11) | (imaxcol.g << 5) | (scale - 1);	\n"
324 "    output.y = (imincol.r << 11) | (imincol.g << 5) | (scale - 1);	\n"
325 "	\n"
326 "    imaxcol.r = (imaxcol.r << 3) | (imaxcol.r >> 2);	\n"
327 "    imaxcol.g = (imaxcol.g << 2) | (imaxcol.g >> 4);	\n"
328 "    imincol.r = (imincol.r << 3) | (imincol.r >> 2);	\n"
329 "    imincol.g = (imincol.g << 2) | (imincol.g >> 4);	\n"
330 "	\n"
331 "    maxcol = (float2)imaxcol * (1.0 / 255.0);	\n"
332 "    mincol = (float2)imincol * (1.0 / 255.0);	\n"
333 "	\n"
334 "    // Undo rescale.	\n"
335 "    maxcol = (maxcol - offset) / scale + offset;	\n"
336 "    mincol = (mincol - offset) / scale + offset;	\n"
337 "	\n"
338 "    return output.x | (output.y << 16);	\n"
339 "}	\n"
340 "	\n"
341 "uint EmitIndicesYCoCgDXT5(float3 block[16], float2 mincol, float2 maxcol)	\n"
342 "{	\n"
343 "    const float COCG_RANGE = 3;	\n"
344 "	\n"
345 "    float2 dir = (maxcol - mincol);	\n"
346 "    float2 origin = maxcol + dir / (2.0 * COCG_RANGE);	\n"
347 "    dir /= dot(dir, dir);	\n"
348 "	\n"
349 "    // Compute indices	\n"
350 "    uint indices = 0;	\n"
351 "    for (int i = 0; i < 16; i++)	\n"
352 "    {	\n"
353 "        uint index = saturate(dot(origin - block[i].yz, dir)) * COCG_RANGE;	\n"
354 "        indices |= index << (i * 2);	\n"
355 "    }	\n"
356 "	\n"
357 "    uint i0 = (indices & 0x55555555);	\n"
358 "    uint i1 = (indices & 0xAAAAAAAA) >> 1;	\n"
359 "    indices = ((i0 ^ i1) << 1) | i1;	\n"
360 "	\n"
361 "    // Output indices	\n"
362 "    return indices;	\n"
363 "}	\n"
364 "	\n"
365 "uint EmitAlphaEndPointsYCoCgDXT5(float mincol, float maxcol)	\n"
366 "{	\n"
367 "    uint c0 = round(mincol * 255);	\n"
368 "    uint c1 = round(maxcol * 255);	\n"
369 "	\n"
370 "    return (c0 << 8) | c1;	\n"
371 "}	\n"
372 "	\n"
373 "// Optimized index selection.	\n"
374 "uint2 EmitAlphaIndicesYCoCgDXT5(float3 block[16], float minAlpha, float maxAlpha)	\n"
375 "{	\n"
376 "    const int ALPHA_RANGE = 7;	\n"
377 "	\n"
378 "    float bias = maxAlpha + (maxAlpha - minAlpha) / (2.0 * ALPHA_RANGE);	\n"
379 "    float scale = 1.0f / (maxAlpha - minAlpha);	\n"
380 "	\n"
381 "    uint2 indices = 0;	\n"
382 "	\n"
383 "    for (int i = 0; i < 6; i++)	\n"
384 "    {	\n"
385 "        uint index = saturate((bias - block[i].x) * scale) * ALPHA_RANGE;	\n"
386 "        indices.x |= index << (3 * i);	\n"
387 "    }	\n"
388 "	\n"
389 "    for (int i = 6; i < 16; i++)	\n"
390 "    {	\n"
391 "        uint index = saturate((bias - block[i].x) * scale) * ALPHA_RANGE;	\n"
392 "        indices.y |= index << (3 * i - 18);	\n"
393 "    }	\n"
394 "	\n"
395 "    uint2 i0 = (indices >> 0) & 0x09249249;	\n"
396 "    uint2 i1 = (indices >> 1) & 0x09249249;	\n"
397 "    uint2 i2 = (indices >> 2) & 0x09249249;	\n"
398 "	\n"
399 "    i2 ^= i0 & i1;	\n"
400 "    i1 ^= i0;	\n"
401 "    i0 ^= (i1 | i2);	\n"
402 "	\n"
403 "    indices.x = (i2.x << 2) | (i1.x << 1) | i0.x;	\n"
404 "    indices.y = (((i2.y << 2) | (i1.y << 1) | i0.y) << 2) | (indices.x >> 16);	\n"
405 "    indices.x <<= 16;	\n"
406 "	\n"
407 "    return indices;	\n"
408 "}	\n"
409 "	\n"
410 "// compress a 4x4 block to DXT1 format	\n"
411 "// integer version, renders to 2 x int32 buffer	\n"
412 "uint4 compress_DXT1_RGBA_fp(float2 texcoord : TEXCOORD0,	\n"
413 "                      uniform sampler2D image,	\n"
414 "                      uniform float2 imageSize = { 512.0, 512.0 }	\n"
415 "                      ) : COLOR	\n"
416 "{	\n"
417 "    // read block	\n"
418 "    float3 block[16];	\n"
419 "    ExtractColorBlockRGB(block, image, texcoord, imageSize);	\n"
420 "	\n"
421 "    // find min and max colors	\n"
422 "    float3 mincol, maxcol;	\n"
423 "    FindMinMaxColorsBox(block, mincol, maxcol);	\n"
424 "	\n"
425 "    SelectDiagonal(block, mincol, maxcol);	\n"
426 "	\n"
427 "    InsetBBox(mincol, maxcol);	\n"
428 "	\n"
429 "    uint4 output;	\n"
430 "    output.x = EmitEndPointsDXT1(mincol, maxcol);	\n"
431 "    output.w = EmitIndicesDXT1(block, mincol, maxcol);	\n"
432 "	\n"
433 "    return output;	\n"
434 "}	\n"
435 "   \n"
436 "uint4 compress_DXT1_BGRA_fp(float2 texcoord : TEXCOORD0,	\n"
437 "                      uniform sampler2D image,	\n"
438 "                      uniform float2 imageSize = { 512.0, 512.0 }	\n"
439 "                      ) : COLOR	\n"
440 "{	\n"
441 "    // read block	\n"
442 "    float3 block[16];	\n"
443 "    ExtractColorBlockBGR(block, image, texcoord, imageSize);	\n"
444 "	\n"
445 "    // find min and max colors	\n"
446 "    float3 mincol, maxcol;	\n"
447 "    FindMinMaxColorsBox(block, mincol, maxcol);	\n"
448 "	\n"
449 "    SelectDiagonal(block, mincol, maxcol);	\n"
450 "	\n"
451 "    InsetBBox(mincol, maxcol);	\n"
452 "	\n"
453 "    uint4 output;	\n"
454 "    output.x = EmitEndPointsDXT1(mincol, maxcol);	\n"
455 "    output.w = EmitIndicesDXT1(block, mincol, maxcol);	\n"
456 "	\n"
457 "   return output;	\n"
458 "}	\n"
459 "	\n"
460 "	\n"
461 "// compress a 4x4 block to YCoCg DXT5 format	\n"
462 "// integer version, renders to 4 x int32 buffer	\n"
463 "uint4 compress_YCoCgDXT5_RGBA_fp(float2 texcoord : TEXCOORD0,	\n"
464 "                      uniform sampler2D image,	\n"
465 "                      uniform float2 imageSize = { 512.0, 512.0 }	\n"
466 "                      ) : COLOR	\n"
467 "{	\n"
468 "    //imageSize = tex2Dsize(image, texcoord);	\n"
469 "	\n"
470 "    // read block	\n"
471 "    float3 block[16];	\n"
472 "    ExtractColorBlockYCoCg_RGB(block, image, texcoord, imageSize);	\n"
473 "	\n"
474 "    // find min and max colors	\n"
475 "    float3 mincol, maxcol;	\n"
476 "    FindMinMaxColorsBox(block, mincol, maxcol);	\n"
477 "	\n"
478 "    SelectYCoCgDiagonal(block, mincol.yz, maxcol.yz);	\n"
479 "	\n"
480 "    int scale = ScaleYCoCg(mincol.yz, maxcol.yz);	\n"
481 "	\n"
482 "    // Output CoCg in DXT1 block.	\n"
483 "    uint4 output;	\n"
484 "    output.z = EmitEndPointsYCoCgDXT5(mincol.yz, maxcol.yz, scale);	\n"
485 "    output.w = EmitIndicesYCoCgDXT5(block, mincol.yz, maxcol.yz);	\n"
486 "	\n"
487 "    InsetYBBox(mincol.x, maxcol.x);	\n"
488 "	\n"
489 "    // Output Y in DXT5 alpha block.	\n"
490 "    output.x = EmitAlphaEndPointsYCoCgDXT5(mincol.x, maxcol.x);	\n"
491 "	\n"
492 "    uint2 indices = EmitAlphaIndicesYCoCgDXT5(block, mincol.x, maxcol.x);	\n"
493 "    output.x |= indices.x;	\n"
494 "    output.y = indices.y;	\n"
495 "	\n"
496 "    return output;	\n"
497 "}	\n"
498 "	\n"
499 "uint4 compress_YCoCgDXT5_BGRA_fp(float2 texcoord : TEXCOORD0,	\n"
500 "                      uniform sampler2D image,	\n"
501 "                      uniform float2 imageSize = { 512.0, 512.0 }	\n"
502 "                      ) : COLOR	\n"
503 "{	\n"
504 "    //imageSize = tex2Dsize(image, texcoord);	\n"
505 "	\n"
506 "    // read block	\n"
507 "    float3 block[16];	\n"
508 "    ExtractColorBlockYCoCg_BGR(block, image, texcoord, imageSize);	\n"
509 "	\n"
510 "    // find min and max colors	\n"
511 "    float3 mincol, maxcol;	\n"
512 "    FindMinMaxColorsBox(block, mincol, maxcol);	\n"
513 "	\n"
514 "    SelectYCoCgDiagonal(block, mincol.yz, maxcol.yz);	\n"
515 "	\n"
516 "    int scale = ScaleYCoCg(mincol.yz, maxcol.yz);	\n"
517 "	\n"
518 "    // Output CoCg in DXT1 block.	\n"
519 "    uint4 output;	\n"
520 "    output.z = EmitEndPointsYCoCgDXT5(mincol.yz, maxcol.yz, scale);	\n"
521 "    output.w = EmitIndicesYCoCgDXT5(block, mincol.yz, maxcol.yz);	\n"
522 "	\n"
523 "    InsetYBBox(mincol.x, maxcol.x);	\n"
524 "	\n"
525 "    // Output Y in DXT5 alpha block.	\n"
526 "    output.x = EmitAlphaEndPointsYCoCgDXT5(mincol.x, maxcol.x);	\n"
527 "	\n"
528 "    uint2 indices = EmitAlphaIndicesYCoCgDXT5(block, mincol.x, maxcol.x);	\n"
529 "    output.x |= indices.x;	\n"
530 "    output.y = indices.y;	\n"
531 "	\n"
532 "    return output;	\n"
533 "}	\n"
534 "	\n"
535 "uniform bool reconstructColor = false;	\n"
536 "uniform bool displayError = false;	\n"
537 "uniform float errorScale = 4.0f;	\n"
538 "	\n"
539 "uniform sampler2D image : TEXUNIT0;	\n"
540 "uniform sampler2D originalImage : TEXUNIT1;	\n"
541 "	\n"
542 "float4 display_fp(float2 texcoord : TEXCOORD0) : COLOR	\n"
543 "{	\n"
544 "    float4 rgba = tex2D(image, texcoord);	\n"
545 "    	\n"
546 "    if (reconstructColor)	\n"
547 "    {	\n"
548 "        float Y = rgba.a;	\n"
549 "        float scale = 1.0 / ((255.0 / 8.0) * rgba.b + 1);	\n"
550 "        float Co = (rgba.r - offset) * scale;	\n"
551 "        float Cg = (rgba.g - offset) * scale;	\n"
552 "	\n"
553 "        float R = Y + Co - Cg;	\n"
554 "        float G = Y + Cg;	\n"
555 "        float B = Y - Co - Cg;	\n"
556 "	\n"
557 "        rgba = float4(R, G, B, 1);	\n"
558 "    }	\n"
559 "	\n"
560 "    if (displayError)	\n"
561 "    {	\n"
562 "        float3 originalColor = tex2D(originalImage, texcoord).rgb;	\n"
563 "        float3 diff = abs(rgba.rgb - originalColor) * errorScale;	\n"
564 "        return float4(diff, 1);	\n"
565 "    }	\n"
566 "    else	\n"
567 "    {	\n"
568 "        return rgba;	\n"
569 "    }	\n"
570 "}	\n"
571 "	\n";