~amaranth/compiz-core/gles

« back to all changes in this revision

Viewing changes to plugins/opengl/src/fragment.cpp

  • Committer: Travis Watkins
  • Date: 2011-08-10 16:56:45 UTC
  • mfrom: (2778.1.37)
  • Revision ID: travis.watkins@linaro.org-20110810165645-y5tmvfyex36bukbo
merge in gles git branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2007 Novell, Inc.
3
 
 *
4
 
 * Permission to use, copy, modify, distribute, and sell this software
5
 
 * and its documentation for any purpose is hereby granted without
6
 
 * fee, provided that the above copyright notice appear in all copies
7
 
 * and that both that copyright notice and this permission notice
8
 
 * appear in supporting documentation, and that the name of
9
 
 * Novell, Inc. not be used in advertising or publicity pertaining to
10
 
 * distribution of the software without specific, written prior permission.
11
 
 * Novell, Inc. makes no representations about the suitability of this
12
 
 * software for any purpose. It is provided "as is" without express or
13
 
 * implied warranty.
14
 
 *
15
 
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16
 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17
 
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18
 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19
 
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20
 
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21
 
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 
 *
23
 
 * Author: David Reveman <davidr@novell.com>
24
 
 */
25
 
 
26
 
#include <boost/function.hpp>
27
 
#include <boost/bind.hpp>
28
 
#include <boost/foreach.hpp>
29
 
#define foreach BOOST_FOREACH
30
 
 
31
 
#include <core/core.h>
32
 
#include <opengl/texture.h>
33
 
#include <opengl/fragment.h>
34
 
#include "privatefragment.h"
35
 
#include "privates.h"
36
 
 
37
 
#include <string.h>
38
 
#include <stdlib.h>
39
 
#include <stdarg.h>
40
 
 
41
 
#define COMP_FUNCTION_TYPE_ARB 0
42
 
#define COMP_FUNCTION_TYPE_NUM 1
43
 
 
44
 
#define COMP_FUNCTION_ARB_MASK (1 << 0)
45
 
#define COMP_FUNCTION_MASK     (COMP_FUNCTION_ARB_MASK)
46
 
 
47
 
namespace GLFragment {
48
 
 
49
 
    class Program {
50
 
        public:
51
 
            Program () :
52
 
                signature (0),
53
 
                blending (false),
54
 
                name (0),
55
 
                type (GL_FRAGMENT_PROGRAM_ARB)
56
 
            {};
57
 
            ~Program ()
58
 
            {
59
 
                if (name)
60
 
                    (*GL::deletePrograms) (1, &name);
61
 
            };
62
 
 
63
 
        public:
64
 
            std::vector<FunctionId> signature;
65
 
 
66
 
            bool blending;
67
 
 
68
 
            GLuint name;
69
 
            GLenum type;
70
 
    };
71
 
 
72
 
    typedef enum {
73
 
        OpTypeData,
74
 
        OpTypeDataStore,
75
 
        OpTypeDataOffset,
76
 
        OpTypeDataBlend,
77
 
        OpTypeHeaderTemp,
78
 
        OpTypeHeaderParam,
79
 
        OpTypeHeaderAttrib,
80
 
        OpTypeColor,
81
 
        OpTypeFetch,
82
 
        OpTypeLoad
83
 
    } OpType;
84
 
 
85
 
    class HeaderOp {
86
 
        public:
87
 
            HeaderOp () : type (OpTypeHeaderTemp), name ("") {};
88
 
        public:
89
 
            OpType     type;
90
 
            CompString name;
91
 
    };
92
 
 
93
 
    class BodyOp {
94
 
        public:
95
 
            BodyOp () :
96
 
                type (OpTypeData),
97
 
                data (""),
98
 
                dst (""),
99
 
                src (""),
100
 
                target (0)
101
 
            {
102
 
                foreach (CompString &str, noOffset)
103
 
                    str = "";
104
 
                foreach (CompString &str, offset)
105
 
                    str = "";
106
 
            };
107
 
 
108
 
        public:
109
 
            OpType       type;
110
 
            CompString   data;
111
 
            CompString   dst;
112
 
            CompString   src;
113
 
            unsigned int target;
114
 
            CompString   noOffset[COMP_FETCH_TARGET_NUM];
115
 
            CompString   offset[COMP_FETCH_TARGET_NUM];
116
 
 
117
 
    };
118
 
 
119
 
    class PrivateFunctionData {
120
 
        public:
121
 
            PrivateFunctionData () : header (0), body (0), status (true) {};
122
 
            PrivateFunctionData (const PrivateFunctionData&, CompString);
123
 
 
124
 
        public:
125
 
            std::vector<HeaderOp> header;
126
 
            std::vector<BodyOp>   body;
127
 
            bool                  status;
128
 
    };
129
 
 
130
 
    class Function {
131
 
        public:
132
 
            Function ():
133
 
                id (0),
134
 
                name (""),
135
 
                mask (0)
136
 
            {};
137
 
 
138
 
        public:
139
 
            FunctionId          id;
140
 
            CompString          name;
141
 
            PrivateFunctionData data[COMP_FUNCTION_TYPE_NUM];
142
 
            unsigned int        mask;
143
 
    };
144
 
 
145
 
    class PrivateAttrib {
146
 
        public:
147
 
            PrivateAttrib () :
148
 
                opacity (0xffff),
149
 
                brightness (0xffff),
150
 
                saturation (0xffff),
151
 
                nTexture (0),
152
 
                nFunction (0),
153
 
                nParam (0)
154
 
            {}
155
 
 
156
 
            PrivateAttrib (const PrivateAttrib &pa) :
157
 
                opacity (pa.opacity),
158
 
                brightness (pa.brightness),
159
 
                saturation (pa.saturation),
160
 
                nTexture (pa.nTexture),
161
 
                nFunction (pa.nFunction),
162
 
                nParam (pa.nParam)
163
 
            {
164
 
                for (int i = 0; i < MAX_FRAGMENT_FUNCTIONS; i++)
165
 
                    function[i] = pa.function[i];
166
 
            }
167
 
 
168
 
        public:
169
 
            GLushort   opacity;
170
 
            GLushort   brightness;
171
 
            GLushort   saturation;
172
 
            int        nTexture;
173
 
            FunctionId function[MAX_FRAGMENT_FUNCTIONS];
174
 
            int        nFunction;
175
 
            int        nParam;
176
 
    };
177
 
 
178
 
    typedef boost::function<void (BodyOp *, int)> DataOpCallBack;
179
 
 
180
 
    class InitialLoadFunction : public Function {
181
 
        public:
182
 
            InitialLoadFunction ()
183
 
            {
184
 
                id   = 0;
185
 
                name = "__core_load";
186
 
                mask = COMP_FUNCTION_MASK;
187
 
 
188
 
                BodyOp b;
189
 
                b.type = OpTypeLoad;
190
 
                b.noOffset[0] = "TEX output, fragment.texcoord[0], texture[0], 2D;";
191
 
                b.noOffset[1] = "TEX output, fragment.texcoord[0], texture[0], RECT;";
192
 
                b.offset[0] = "TEX output, __tmp_texcoord0, texture[0], 2D;";
193
 
                b.offset[1] = "TEX output, __tmp_texcoord0, texture[0], RECT;";
194
 
                data[0].body.push_back (b);
195
 
            };
196
 
    };
197
 
 
198
 
    static InitialLoadFunction initialLoadFunction;
199
 
 
200
 
    static Function *
201
 
    findFragmentFunction (GLScreen   *s,
202
 
                          FunctionId id)
203
 
    {
204
 
        foreach (Function *f, s->fragmentStorage ()->functions)
205
 
            if (f->id == id)
206
 
                return f;
207
 
        return NULL;
208
 
    }
209
 
 
210
 
    static Function *
211
 
    findFragmentFunctionWithName (GLScreen   *s,
212
 
                                  CompString name)
213
 
    {
214
 
        foreach (Function *f, s->fragmentStorage ()->functions)
215
 
            if (f->name.compare (name) == 0)
216
 
                return f;
217
 
        return NULL;
218
 
    }
219
 
 
220
 
    static Program *
221
 
    findFragmentProgram (GLScreen     *s,
222
 
                         FunctionId   *signature,
223
 
                         unsigned int nSignature)
224
 
    {
225
 
        unsigned int i;
226
 
 
227
 
        foreach (Program *p, s->fragmentStorage ()->programs)
228
 
        {
229
 
            if (p->signature.size () != nSignature)
230
 
                continue;
231
 
 
232
 
            for (i = 0; i < nSignature; i++)
233
 
                if (signature[i] != p->signature[i])
234
 
                    break;
235
 
 
236
 
            if (i == nSignature)
237
 
                return p;
238
 
        }
239
 
        return NULL;
240
 
    }
241
 
 
242
 
    static unsigned int
243
 
    functionMaskToType (int mask)
244
 
    {
245
 
        static struct {
246
 
            unsigned int type;
247
 
            unsigned int mask;
248
 
        } maskToType[] = {
249
 
            { COMP_FUNCTION_TYPE_ARB, COMP_FUNCTION_ARB_MASK }
250
 
        };
251
 
 
252
 
        unsigned int i;
253
 
 
254
 
        for (i = 0; i < sizeof (maskToType) / sizeof (maskToType[0]); i++)
255
 
            if (mask & maskToType[i].mask)
256
 
                return maskToType[i].type;
257
 
 
258
 
        return 0;
259
 
    }
260
 
 
261
 
    static void
262
 
    forEachDataOpInFunction (std::vector<Function *> list,
263
 
                             int                     index,
264
 
                             int                     type,
265
 
                             int                     loadTarget,
266
 
                             CompString              loadOffset,
267
 
                             bool                    *color,
268
 
                             bool                    *blend,
269
 
                             DataOpCallBack          callBack)
270
 
    {
271
 
        Function *f = list[index];
272
 
        BodyOp   dataOp;
273
 
        bool     colorDone = false;
274
 
        bool     blendDone = false;
275
 
 
276
 
        *color = false;
277
 
        *blend = false;
278
 
 
279
 
        foreach (BodyOp &bodyOp, f->data[type].body)
280
 
        {
281
 
            switch (bodyOp.type) {
282
 
                case OpTypeFetch: {
283
 
                    CompString offset = loadOffset;
284
 
 
285
 
                    /* add offset */
286
 
                    if (bodyOp.data.size ())
287
 
                    {
288
 
                        if (loadOffset.size ())
289
 
                        {
290
 
                            dataOp.type = OpTypeDataOffset;
291
 
                            dataOp.data =
292
 
                                compPrintf ("ADD __tmp_texcoord%d, %s, %s;",
293
 
                                            index, loadOffset.c_str (),
294
 
                                            bodyOp.data.c_str ());
295
 
 
296
 
                            callBack (&dataOp, index);
297
 
 
298
 
                            offset = compPrintf ("__tmp_texcoord%d", index);
299
 
                        }
300
 
                        else
301
 
                        {
302
 
                            offset = bodyOp.data;
303
 
                        }
304
 
                    }
305
 
 
306
 
                    forEachDataOpInFunction (list, index - 1, type,
307
 
                                            bodyOp.target,
308
 
                                            offset, &colorDone, &blendDone,
309
 
                                            callBack);
310
 
 
311
 
                    if (bodyOp.dst.compare ("output"))
312
 
                    {
313
 
                        dataOp.type = OpTypeDataStore;
314
 
                        dataOp.data =
315
 
                            compPrintf ("MOV %s, output;", bodyOp.dst.c_str ());
316
 
 
317
 
                        /* move to destination */
318
 
                        callBack (&dataOp, index);
319
 
                    }
320
 
                } break;
321
 
                case OpTypeLoad:
322
 
                    if (loadOffset.size ())
323
 
                    {
324
 
                        dataOp.type = OpTypeDataOffset;
325
 
                        dataOp.data =
326
 
                            compPrintf ("ADD __tmp_texcoord0, fragment.texcoord[0], %s;",
327
 
                                        loadOffset.c_str ());
328
 
 
329
 
                        callBack (&dataOp, index);
330
 
 
331
 
                        dataOp.data = bodyOp.offset[loadTarget];
332
 
                    }
333
 
                    else
334
 
                    {
335
 
                        dataOp.data = bodyOp.noOffset[loadTarget];
336
 
                    }
337
 
 
338
 
                    dataOp.type = OpTypeData;
339
 
 
340
 
                    callBack (&dataOp, index);
341
 
 
342
 
                    break;
343
 
                case OpTypeColor:
344
 
                    if (!colorDone)
345
 
                    {
346
 
                        dataOp.type = OpTypeData;
347
 
                        dataOp.data =
348
 
                            compPrintf ("MUL %s, fragment.color, %s;",
349
 
                                        bodyOp.dst.c_str (),
350
 
                                        bodyOp.src.c_str ());
351
 
 
352
 
                        callBack (&dataOp, index);
353
 
                    }
354
 
                    else if (bodyOp.dst.compare (bodyOp.src))
355
 
                    {
356
 
                        dataOp.type = OpTypeData;
357
 
                        dataOp.data =
358
 
                            compPrintf ("MOV %s, %s;",
359
 
                                        bodyOp.dst.c_str (),
360
 
                                        bodyOp.src.c_str ());
361
 
 
362
 
                        callBack (&dataOp, index);
363
 
                    }
364
 
                    *color = true;
365
 
                    break;
366
 
                case OpTypeDataBlend:
367
 
                    *blend = true;
368
 
                    /* fall-through */
369
 
                case OpTypeData:
370
 
                    callBack (&bodyOp, index);
371
 
                    break;
372
 
                case OpTypeDataStore:
373
 
                case OpTypeDataOffset:
374
 
                case OpTypeHeaderTemp:
375
 
                case OpTypeHeaderParam:
376
 
                case OpTypeHeaderAttrib:
377
 
                    break;
378
 
            }
379
 
        }
380
 
 
381
 
        if (colorDone)
382
 
            *color = true;
383
 
 
384
 
        if (blendDone)
385
 
            *blend = true;
386
 
    }
387
 
 
388
 
    static int
389
 
    forEachHeaderOpWithType (std::vector<HeaderOp> list,
390
 
                             int                   index,
391
 
                             OpType                type,
392
 
                             CompString            prefix,
393
 
                             CompString            functionPrefix,
394
 
                             int                   count,
395
 
                             DataOpCallBack        callBack)
396
 
    {
397
 
        BodyOp dataOp;
398
 
 
399
 
        dataOp.type = OpTypeData;
400
 
 
401
 
        foreach (HeaderOp &header, list)
402
 
        {
403
 
            if (header.type == type)
404
 
            {
405
 
                if (count)
406
 
                {
407
 
                    dataOp.data = ", ";
408
 
                }
409
 
                else
410
 
                {
411
 
                    dataOp.data = prefix;
412
 
                }
413
 
 
414
 
                dataOp.data += functionPrefix;
415
 
                dataOp.data += "_";
416
 
                dataOp.data += header.name;
417
 
 
418
 
                callBack (&dataOp, index);
419
 
 
420
 
                count++;
421
 
            }
422
 
        }
423
 
 
424
 
        return count;
425
 
    }
426
 
 
427
 
    static bool
428
 
    forEachDataOp (std::vector<Function *> list,
429
 
                   int                     type,
430
 
                   DataOpCallBack          callBack)
431
 
    {
432
 
        BodyOp dataOp;
433
 
        bool   colorDone;
434
 
        bool   blendDone;
435
 
        int    count, nList = list.size ();
436
 
 
437
 
        dataOp.type = OpTypeData;
438
 
 
439
 
        count = 1;
440
 
 
441
 
        dataOp.data = "TEMP output";
442
 
 
443
 
        callBack (&dataOp, nList);
444
 
 
445
 
        foreach (Function *f, list)
446
 
            count = forEachHeaderOpWithType (f->data[type].header,
447
 
                                             nList, OpTypeHeaderTemp,
448
 
                                             "", f->name, count, callBack);
449
 
 
450
 
        dataOp.data = ";";
451
 
 
452
 
        callBack (&dataOp, nList);
453
 
 
454
 
        count = 0;
455
 
 
456
 
        foreach (Function *f, list)
457
 
            count = forEachHeaderOpWithType (f->data[type].header,
458
 
                                             nList, OpTypeHeaderParam,
459
 
                                             "PARAM ", f->name, count,
460
 
                                             callBack);
461
 
 
462
 
        if (count)
463
 
        {
464
 
            dataOp.data = ";";
465
 
 
466
 
            callBack (&dataOp, nList);
467
 
        }
468
 
 
469
 
        count = 0;
470
 
 
471
 
        foreach (Function *f, list)
472
 
            count = forEachHeaderOpWithType (f->data[type].header,
473
 
                                             nList, OpTypeHeaderAttrib,
474
 
                                             "ATTRIB ", f->name, count,
475
 
                                             callBack);
476
 
 
477
 
        if (count)
478
 
        {
479
 
            dataOp.data = ";";
480
 
 
481
 
            callBack (&dataOp, nList);
482
 
        }
483
 
 
484
 
        forEachDataOpInFunction (list, nList - 1, type, 0, "",
485
 
                                 &colorDone, &blendDone,
486
 
                                 callBack);
487
 
 
488
 
        if (colorDone)
489
 
            dataOp.data = "MOV result.color, output;END";
490
 
        else
491
 
            dataOp.data = "MUL result.color, fragment.color, output;END";
492
 
 
493
 
        callBack (&dataOp, nList);
494
 
 
495
 
        return blendDone;
496
 
    }
497
 
 
498
 
    static void
499
 
    addFetchOffsetVariables (BodyOp     *op,
500
 
                             int        index,
501
 
                             bool       *indices,
502
 
                             CompString *data)
503
 
    {
504
 
        if (op->type == OpTypeDataOffset)
505
 
        {
506
 
            if (!indices[index])
507
 
            {
508
 
                data->append (compPrintf ("TEMP __tmp_texcoord%d;", index));
509
 
                indices[index] = true;
510
 
            }
511
 
        }
512
 
    }
513
 
 
514
 
    static void
515
 
    addData (BodyOp     *op,
516
 
             CompString *data)
517
 
    {
518
 
        data->append (op->data);
519
 
    }
520
 
 
521
 
    static Program *
522
 
    buildFragmentProgram (GLScreen      *s,
523
 
                          PrivateAttrib *attrib)
524
 
    {
525
 
        Program                 *program;
526
 
        std::vector<Function *> functionList (1);
527
 
        int                     mask = COMP_FUNCTION_MASK;
528
 
        int                     type;
529
 
        GLint                   errorPos;
530
 
        GLenum                  errorType;
531
 
        CompString fetchData;
532
 
        bool       indices[MAX_FRAGMENT_FUNCTIONS];
533
 
        int        i;
534
 
 
535
 
        program = new Program ();
536
 
        if (!program)
537
 
            return NULL;
538
 
 
539
 
        functionList[0] = &initialLoadFunction;
540
 
 
541
 
        for (i = 0; i < attrib->nFunction; i++)
542
 
        {
543
 
            Function *f = findFragmentFunction (s, attrib->function[i]);
544
 
 
545
 
            if (f)
546
 
                functionList.push_back (f);
547
 
        }
548
 
 
549
 
        foreach (Function *f, functionList)
550
 
            mask &= f->mask;
551
 
 
552
 
        if (!mask)
553
 
        {
554
 
            compLogMessage ("opengl", CompLogLevelWarn,
555
 
                            "fragment functions can't be linked together "
556
 
                            "because a common type doesn't exist");
557
 
        }
558
 
 
559
 
        if (!mask || functionList.size () == 1)
560
 
        {
561
 
            delete program;
562
 
            return NULL;
563
 
        }
564
 
 
565
 
        for (i = 0; i < attrib->nFunction; i++)
566
 
            program->signature.push_back (attrib->function[i]);
567
 
 
568
 
        type = functionMaskToType (mask);
569
 
 
570
 
        fetchData = "!!ARBfp1.0";
571
 
 
572
 
        foreach (bool &val, indices)
573
 
            val = false;
574
 
 
575
 
        forEachDataOp (functionList, type,
576
 
            boost::bind (addFetchOffsetVariables, _1, _2, indices, &fetchData));
577
 
 
578
 
        program->blending = forEachDataOp (functionList, type,
579
 
                                boost::bind (addData, _1, &fetchData));
580
 
 
581
 
        program->type = GL_FRAGMENT_PROGRAM_ARB;
582
 
 
583
 
        glGetError ();
584
 
 
585
 
        (*GL::genPrograms) (1, &program->name);
586
 
        (*GL::bindProgram) (GL_FRAGMENT_PROGRAM_ARB, program->name);
587
 
        (*GL::programString) (GL_FRAGMENT_PROGRAM_ARB,
588
 
                              GL_PROGRAM_FORMAT_ASCII_ARB,
589
 
                              fetchData.size (), fetchData.c_str ());
590
 
 
591
 
        glGetIntegerv (GL_PROGRAM_ERROR_POSITION_ARB, &errorPos);
592
 
        errorType = glGetError ();
593
 
        if (errorType != GL_NO_ERROR || errorPos != -1)
594
 
        {
595
 
            compLogMessage ("opengl", CompLogLevelError,
596
 
                            "failed to load fragment program");
597
 
 
598
 
            (*GL::deletePrograms) (1, &program->name);
599
 
 
600
 
            program->name = 0;
601
 
            program->type = 0;
602
 
        }
603
 
 
604
 
        return program;
605
 
    }
606
 
 
607
 
    static GLuint
608
 
    getFragmentProgram (GLScreen      *s,
609
 
                        PrivateAttrib *attrib,
610
 
                        GLenum        *type,
611
 
                        bool          *blending)
612
 
    {
613
 
        Program *program;
614
 
 
615
 
        if (!attrib->nFunction)
616
 
            return 0;
617
 
 
618
 
        program = findFragmentProgram (s, attrib->function, attrib->nFunction);
619
 
        if (!program)
620
 
        {
621
 
            program = buildFragmentProgram (s, attrib);
622
 
            if (program)
623
 
            {
624
 
                s->fragmentStorage ()->programs.push_back (program);
625
 
            }
626
 
        }
627
 
 
628
 
        if (program)
629
 
        {
630
 
            *type     = program->type;
631
 
            *blending = program->blending;
632
 
 
633
 
            return program->name;
634
 
        }
635
 
 
636
 
        return 0;
637
 
    }
638
 
 
639
 
 
640
 
    /* performs simple variable substitution */
641
 
    static CompString
642
 
    copyData (std::vector<HeaderOp> header,
643
 
              const CompString      prefix,
644
 
              CompString            data)
645
 
    {
646
 
        CompString inPrefix (prefix);
647
 
        inPrefix += "_";
648
 
 
649
 
        foreach (HeaderOp &h, header)
650
 
        {
651
 
            size_t pos = data.find (h.name);
652
 
            while (pos != std::string::npos)
653
 
            {
654
 
                bool prependPrefix = false;
655
 
                /* It is possible to match parts of words here, so
656
 
                 * make sure that we have found the next chunk in the
657
 
                 * string and not just a header which matches
658
 
                 * part of another word */
659
 
                if (data.size () > pos + h.name.size ())
660
 
                {
661
 
                    const CompString &token = data.substr (pos + h.name.size (), 1);
662
 
                    if (token == "," ||
663
 
                        token == "." ||
664
 
                        token == ";")
665
 
                    {
666
 
                        prependPrefix = true;
667
 
                    }
668
 
                    else
669
 
                    {
670
 
                        /* We matched part of another word as our
671
 
                         * token so search for the next whole
672
 
                         * header op */
673
 
                        pos = data.find (h.name, pos + 1);
674
 
                    }
675
 
                }
676
 
                else
677
 
                {
678
 
                    /* If this is the last word in the string, then it must
679
 
                     * have matched exactly our header op, so it is ok
680
 
                     * to prepend a prefix here and go straight to
681
 
                     * std::string::npos */
682
 
                    prependPrefix = true;
683
 
                }
684
 
 
685
 
                if (prependPrefix)
686
 
                {
687
 
                    /* prepend the header op prefix to the header op
688
 
                     * and seek past this word to the next instance
689
 
                     * of the unprepended header op */
690
 
                    data.insert (pos, inPrefix);
691
 
                    pos += inPrefix.size () + h.name.size ();
692
 
                    pos = data.find (h.name, pos);
693
 
                }
694
 
            }
695
 
        }
696
 
 
697
 
        return data;
698
 
    }
699
 
 
700
 
    PrivateFunctionData::PrivateFunctionData (const PrivateFunctionData& src,
701
 
                                              CompString dstPrefix) :
702
 
        header (src.header),
703
 
        body (0),
704
 
        status (src.status)
705
 
    {
706
 
 
707
 
        foreach (BodyOp b, src.body)
708
 
        {
709
 
            BodyOp dst;
710
 
            dst.type = b.type;
711
 
 
712
 
            switch (b.type) {
713
 
                case OpTypeFetch:
714
 
                    dst.dst = copyData (header, dstPrefix, b.dst);
715
 
                    if (b.data.size ())
716
 
                        dst.data = copyData (header, dstPrefix, b.data);
717
 
                    else
718
 
                        dst.data = "";
719
 
 
720
 
                    dst.target = b.target;
721
 
                    break;
722
 
                case OpTypeLoad:
723
 
                case OpTypeHeaderTemp:
724
 
                case OpTypeHeaderParam:
725
 
                case OpTypeHeaderAttrib:
726
 
                    break;
727
 
                case OpTypeData:
728
 
                case OpTypeDataBlend:
729
 
                case OpTypeDataStore:
730
 
                case OpTypeDataOffset:
731
 
                    dst.data = copyData (header, dstPrefix, b.data);
732
 
                    break;
733
 
                case OpTypeColor:
734
 
                    dst.dst = copyData (header, dstPrefix, b.dst);
735
 
                    dst.src = copyData (header, dstPrefix, b.src);
736
 
                    break;
737
 
                }
738
 
            body.push_back (dst);
739
 
        }
740
 
    }
741
 
 
742
 
    static bool
743
 
    addHeaderOpToFunctionData (PrivateFunctionData *data,
744
 
                               const char          *name,
745
 
                               OpType              type)
746
 
    {
747
 
        static const char *reserved[] = {
748
 
            "output",
749
 
            "__tmp_texcoord",
750
 
            "fragment",
751
 
            "program",
752
 
            "result",
753
 
            "state",
754
 
            "texture"
755
 
        };
756
 
        HeaderOp   header;
757
 
        CompString n (name);
758
 
 
759
 
        foreach (const char *word, reserved)
760
 
        {
761
 
            if (n.find (word) != std::string::npos)
762
 
            {
763
 
                compLogMessage ("opengl", CompLogLevelWarn,
764
 
                                "%s is a reserved word", word);
765
 
                return false;
766
 
            }
767
 
        }
768
 
 
769
 
 
770
 
        header.type = type;
771
 
        header.name = n;
772
 
        data->header.push_back (header);
773
 
 
774
 
        return true;
775
 
    }
776
 
 
777
 
    FunctionData::FunctionData () :
778
 
        priv (new PrivateFunctionData ())
779
 
    {
780
 
    }
781
 
 
782
 
    FunctionData::~FunctionData ()
783
 
    {
784
 
        delete priv;
785
 
    }
786
 
 
787
 
    bool
788
 
    FunctionData::status ()
789
 
    {
790
 
        return priv->status;
791
 
    }
792
 
 
793
 
    void
794
 
    FunctionData::addTempHeaderOp (const char *name)
795
 
    {
796
 
        priv->status &=
797
 
            addHeaderOpToFunctionData (priv, name, OpTypeHeaderTemp);
798
 
    }
799
 
 
800
 
    void
801
 
    FunctionData::addParamHeaderOp (const char *name)
802
 
    {
803
 
        priv->status &=
804
 
            addHeaderOpToFunctionData (priv, name, OpTypeHeaderParam);
805
 
    }
806
 
 
807
 
    void
808
 
    FunctionData::addAttribHeaderOp (const char *name)
809
 
    {
810
 
        priv->status &=
811
 
            addHeaderOpToFunctionData (priv, name, OpTypeHeaderAttrib);
812
 
    }
813
 
 
814
 
 
815
 
    void
816
 
    FunctionData::addFetchOp (const char *dst, const char *offset, int target)
817
 
    {
818
 
        BodyOp b;
819
 
 
820
 
        b.type   = OpTypeFetch;
821
 
        b.dst    = CompString (dst);
822
 
        b.target = target;
823
 
 
824
 
        if (offset)
825
 
            b.data = CompString (offset);
826
 
        else
827
 
            b.data = CompString ("");
828
 
 
829
 
        priv->body.push_back (b);
830
 
    }
831
 
 
832
 
    void
833
 
    FunctionData::addColorOp (const char *dst, const char *src)
834
 
    {
835
 
        BodyOp b;
836
 
 
837
 
        b.type = OpTypeColor;
838
 
        b.dst  = CompString (dst);
839
 
        b.src  = CompString (src);
840
 
 
841
 
        priv->body.push_back (b);
842
 
    }
843
 
 
844
 
    void
845
 
    FunctionData::addDataOp (const char *str, ...)
846
 
    {
847
 
        BodyOp  b;
848
 
        va_list ap;
849
 
 
850
 
        b.type = OpTypeData;
851
 
        va_start (ap, str);
852
 
        b.data = compPrintf (str, ap);
853
 
        va_end (ap);
854
 
 
855
 
        priv->body.push_back (b);
856
 
    }
857
 
 
858
 
    void
859
 
    FunctionData::addBlendOp (const char *str, ...)
860
 
    {
861
 
        BodyOp  b;
862
 
        va_list ap;
863
 
 
864
 
        b.type = OpTypeDataBlend;
865
 
        va_start (ap, str);
866
 
        b.data = compPrintf (str, ap);
867
 
        va_end (ap);
868
 
 
869
 
        priv->body.push_back (b);
870
 
    }
871
 
 
872
 
    FunctionId
873
 
    FunctionData::createFragmentFunction (const char *name)
874
 
    {
875
 
        GLScreen     *s = GLScreen::get (screen);
876
 
        Function     *function = new Function ();
877
 
        CompString   validName = name;
878
 
        unsigned int i = 0;
879
 
 
880
 
        while (findFragmentFunctionWithName (s, validName))
881
 
        {
882
 
            validName = compPrintf ("%s%d", name, i++);
883
 
        }
884
 
 
885
 
        function->data[COMP_FUNCTION_TYPE_ARB] =
886
 
            PrivateFunctionData (*priv, validName);
887
 
 
888
 
        function->name = validName;
889
 
        function->mask = COMP_FUNCTION_ARB_MASK;
890
 
        function->id   = s->fragmentStorage ()->lastFunctionId++;
891
 
 
892
 
        s->fragmentStorage ()->functions.push_back (function);
893
 
 
894
 
        return function->id;
895
 
    }
896
 
 
897
 
    Attrib::Attrib (const GLWindowPaintAttrib &paint) :
898
 
        priv (new PrivateAttrib ())
899
 
    {
900
 
        priv->opacity    = paint.opacity;
901
 
        priv->brightness = paint.brightness;
902
 
        priv->saturation = paint.saturation;
903
 
        priv->nTexture   = 0;
904
 
        priv->nFunction  = 0;
905
 
        priv->nParam     = 0;
906
 
 
907
 
        foreach (FunctionId &f, priv->function)
908
 
            f = 0;
909
 
    }
910
 
 
911
 
    Attrib::Attrib (const Attrib &fa) :
912
 
        priv (new PrivateAttrib (*fa.priv))
913
 
    {
914
 
    }
915
 
 
916
 
    Attrib::~Attrib ()
917
 
    {
918
 
        delete priv;
919
 
    }
920
 
 
921
 
    Attrib &
922
 
    Attrib::operator= (const Attrib &rhs)
923
 
    {
924
 
        if (this == &rhs) // Check for self-assignment
925
 
            return *this;
926
 
 
927
 
        delete priv;
928
 
        priv = new PrivateAttrib (*rhs.priv);
929
 
 
930
 
        return *this;
931
 
    }
932
 
 
933
 
    unsigned int
934
 
    Attrib::allocTextureUnits (unsigned int nTexture)
935
 
    {
936
 
        unsigned int first = priv->nTexture;
937
 
 
938
 
        priv->nTexture += nTexture;
939
 
 
940
 
        /* 0 is reserved for source texture */
941
 
        return 1 + first;
942
 
    }
943
 
 
944
 
    unsigned int
945
 
    Attrib::allocParameters (unsigned int nParam)
946
 
    {
947
 
        unsigned int first = priv->nParam;
948
 
 
949
 
        priv->nParam += nParam;
950
 
 
951
 
        return first;
952
 
    }
953
 
 
954
 
    void
955
 
    Attrib::addFunction (FunctionId function)
956
 
    {
957
 
        if (priv->nFunction < MAX_FRAGMENT_FUNCTIONS)
958
 
            priv->function[priv->nFunction++] = function;
959
 
    }
960
 
 
961
 
    bool
962
 
    Attrib::enable (bool *blending)
963
 
    {
964
 
        GLuint name;
965
 
        GLenum type;
966
 
        bool   programBlending;
967
 
 
968
 
        if (!GL::fragmentProgram)
969
 
            return false;
970
 
 
971
 
        name = getFragmentProgram (GLScreen::get (screen), priv, &type,
972
 
                                   &programBlending);
973
 
        if (!name)
974
 
            return false;
975
 
 
976
 
        *blending = !programBlending;
977
 
 
978
 
        glEnable (GL_FRAGMENT_PROGRAM_ARB);
979
 
 
980
 
        (*GL::bindProgram) (type, name);
981
 
 
982
 
        return true;
983
 
    }
984
 
 
985
 
    void
986
 
    Attrib::disable ()
987
 
    {
988
 
        glDisable (GL_FRAGMENT_PROGRAM_ARB);
989
 
    }
990
 
 
991
 
    unsigned short
992
 
    Attrib::getSaturation ()
993
 
    {
994
 
        return priv->saturation;
995
 
    }
996
 
 
997
 
    unsigned short
998
 
    Attrib::getBrightness ()
999
 
    {
1000
 
        return priv->brightness;
1001
 
    }
1002
 
 
1003
 
    unsigned short
1004
 
    Attrib::getOpacity ()
1005
 
    {
1006
 
        return priv->opacity;
1007
 
    }
1008
 
 
1009
 
    void
1010
 
    Attrib::setSaturation (unsigned short value)
1011
 
    {
1012
 
        priv->saturation = value;
1013
 
    }
1014
 
 
1015
 
    void
1016
 
    Attrib::setBrightness (unsigned short value)
1017
 
        {
1018
 
        priv->brightness = value;
1019
 
    }
1020
 
 
1021
 
 
1022
 
    void
1023
 
    Attrib::setOpacity (unsigned short value)
1024
 
    {
1025
 
        priv->opacity = value;
1026
 
    }
1027
 
 
1028
 
    bool
1029
 
    Attrib::hasFunctions ()
1030
 
    {
1031
 
        return priv->nFunction > 0;
1032
 
    }
1033
 
 
1034
 
    void destroyFragmentFunction (FunctionId id)
1035
 
    {
1036
 
        GLScreen *s = GLScreen::get (screen);
1037
 
        Function *function;
1038
 
        Program  *program;
1039
 
 
1040
 
        function = findFragmentFunction (s, id);
1041
 
 
1042
 
        if (!function)
1043
 
            return;
1044
 
 
1045
 
        std::vector<Program *>::iterator it;
1046
 
 
1047
 
        do {
1048
 
            program = NULL;
1049
 
 
1050
 
            it = s->fragmentStorage ()->programs.begin ();
1051
 
 
1052
 
            for (; it != s->fragmentStorage ()->programs.end (); it++)
1053
 
            {
1054
 
                foreach (FunctionId i, (*it)->signature)
1055
 
                    if (i == id)
1056
 
                    {
1057
 
                        program = (*it);
1058
 
                        break;
1059
 
                    }
1060
 
 
1061
 
                if (program)
1062
 
                    break;
1063
 
            }
1064
 
 
1065
 
            if (program)
1066
 
            {
1067
 
                delete program;
1068
 
                s->fragmentStorage ()->programs.erase (it);
1069
 
            }
1070
 
 
1071
 
        } while (program);
1072
 
 
1073
 
        std::vector<Function *>::iterator fi =
1074
 
            std::find (s->fragmentStorage ()->functions.begin (),
1075
 
                       s->fragmentStorage ()->functions.end (),
1076
 
                       function);
1077
 
        if (fi != s->fragmentStorage ()->functions.end ())
1078
 
            s->fragmentStorage ()->functions.erase (fi);
1079
 
 
1080
 
        delete (function);
1081
 
    }
1082
 
 
1083
 
    FunctionId
1084
 
    getSaturateFragmentFunction (GLTexture *texture,
1085
 
                                 int         param)
1086
 
    {
1087
 
        int      target;
1088
 
        GLScreen *s = GLScreen::get (screen);
1089
 
 
1090
 
        if (param >= 64)
1091
 
            return 0;
1092
 
 
1093
 
        if (texture->target () == GL_TEXTURE_2D)
1094
 
            target = COMP_FETCH_TARGET_2D;
1095
 
        else
1096
 
            target = COMP_FETCH_TARGET_RECT;
1097
 
 
1098
 
        if (!s->fragmentStorage ()->saturateFunction [target][param])
1099
 
        {
1100
 
            static const char *saturateData =
1101
 
                "MUL temp, output, { 1.0, 1.0, 1.0, 0.0 };"
1102
 
                "DP3 temp, temp, program.env[%d];"
1103
 
                "LRP output.xyz, program.env[%d].w, output, temp;";
1104
 
            FunctionData  data;
1105
 
 
1106
 
            data.addTempHeaderOp ("temp");
1107
 
            data.addFetchOp ("output", NULL, target);
1108
 
            data.addColorOp ("output", "output");
1109
 
 
1110
 
            data.addDataOp (saturateData, param, param);
1111
 
 
1112
 
            if (!data.status ())
1113
 
                return 0;
1114
 
 
1115
 
            s->fragmentStorage ()->saturateFunction [target][param] =
1116
 
                data.createFragmentFunction ("__core_saturate");
1117
 
 
1118
 
        }
1119
 
 
1120
 
        return s->fragmentStorage ()->saturateFunction [target][param];
1121
 
    }
1122
 
 
1123
 
    Storage::Storage () :
1124
 
        lastFunctionId (1),
1125
 
        functions (0),
1126
 
        programs (0)
1127
 
    {
1128
 
        for (int i = 0; i < 64; i++)
1129
 
        {
1130
 
            saturateFunction[0][i] = 0;
1131
 
            saturateFunction[1][i] = 0;
1132
 
        }
1133
 
    }
1134
 
 
1135
 
    Storage::~Storage ()
1136
 
    {
1137
 
        foreach (Program *p, programs)
1138
 
            delete p;
1139
 
        programs.clear ();
1140
 
        foreach (Function *f, functions)
1141
 
            delete f;
1142
 
        functions.clear ();
1143
 
    }
1144
 
 
1145
 
};