Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
///////////////////////////////////////////////////////////////////////////////
// Written by Philip Rideout in April 2010
// Covered by the MIT License

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "Headers/bstrlib.h"
#include "Headers/glsw.h"

#ifdef WIN32
#pragma warning(disable:4996) // allow "fopen"
#endif

///////////////////////////////////////////////////////////////////////////////
// PRIVATE TYPES

typedef struct glswListRec
{
    bstring Key;
    bstring Value;
    struct glswListRec* Next;
} glswList;

typedef struct glswContextRec
{
    bstring PathPrefix;
    bstring PathSuffix;
    bstring ErrorMessage;
    glswList* TokenMap;
    glswList* ShaderMap;
    glswList* LoadedEffects;
} glswContext;

///////////////////////////////////////////////////////////////////////////////
// PRIVATE GLOBALS

static glswContext* __glsw__Context = 0;

///////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS

static int __glsw__Alphanumeric(char c)
{
    return
        (c >= 'A' && c <= 'Z') ||
        (c >= 'a' && c <= 'z') ||
        (c >= '0' && c <= '9') ||
        c == '_' || c == '.';
}

static void __glsw__FreeList(glswList* pNode)
{
    while (pNode)
    {
        glswList* pNext = pNode->Next;
        bdestroy(pNode->Key);
        bdestroy(pNode->Value);
        free(pNode);
        pNode = pNext;
    }
}

///////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS

static int glswClear(glswContext* gc)
{
    __glsw__FreeList(gc->ShaderMap);
    __glsw__FreeList(gc->LoadedEffects);
    gc->ShaderMap = NULL;
    gc->LoadedEffects = NULL;
    return 0;
}

void glswClearCurrentContext()
{
    glswClear(__glsw__Context);
}

int glswInit()
{
    if (__glsw__Context)
    {
        bdestroy(__glsw__Context->ErrorMessage);
        __glsw__Context->ErrorMessage = bfromcstr("Already initialized.");
        return 0;
    }

    __glsw__Context = (glswContext*) calloc(sizeof(glswContext), 1);

    return 1;
}

int glswShutdown()
{
    glswContext* gc = __glsw__Context;

    if (!gc)
    {
        return 0;
    }

    bdestroy(gc->PathPrefix);
    bdestroy(gc->PathSuffix);
    bdestroy(gc->ErrorMessage);
    glswClear(gc);
    __glsw__FreeList(gc->TokenMap);
    gc->TokenMap = NULL;
    free(gc);
    __glsw__Context = 0;

    return 1;
}

int glswSetPath(const char* pathPrefix, const char* pathSuffix)
{
    glswContext* gc = __glsw__Context;

    if (!gc)
    {
        return 0;
    }

    if (!gc->PathPrefix) {
        gc->PathPrefix = bfromcstr(pathPrefix);
    } else {
        bstring prefix = bfromcstr(pathPrefix);
        if (bstrcmp(gc->PathPrefix, prefix) != 0) {
            bassign(gc->PathPrefix, prefix);
        }
        else {
            bdestroy(prefix);
        }
    }

    if (!gc->PathSuffix) {
        gc->PathSuffix = bfromcstr(pathSuffix);
    } else {
        bstring suffix = bfromcstr(pathSuffix);
        if (bstrcmp(gc->PathSuffix, suffix) != 0) {
            bassign(gc->PathSuffix, suffix);
        }
        else {
            bdestroy(suffix);
        }
    }

    return 1;
}

const char* glswGetShader(const char* pEffectKey)
{
    glswContext* gc = __glsw__Context;

    bstring effectKey = 0;
    glswList* closestMatch = 0;
    struct bstrList* tokens = 0;
    bstring effectName = 0;
    glswList* pLoadedEffect = 0;
    glswList* pShaderEntry = 0;
    bstring shaderKey = 0;

    if (!gc) {
        return 0;
    }

    // Extract the effect name from the effect key
    effectKey = bfromcstr(pEffectKey);
    tokens = bsplit(effectKey, '.');
    if (!tokens || !tokens->qty)
    {
        bdestroy(gc->ErrorMessage);
        gc->ErrorMessage = bformat("Malformed effect key key '%s'.", pEffectKey);
        bstrListDestroy(tokens);
        bdestroy(effectKey);
        return 0;
    }
    effectName = tokens->entry[0];

    // Check if we already loaded this effect file
    pLoadedEffect = gc->LoadedEffects;
    while (pLoadedEffect) {
        if (biseq(pLoadedEffect->Key, effectName) == 1)  {
            break;
        }
        pLoadedEffect = pLoadedEffect->Next;
    }

    // If we haven't loaded this file yet, load it in
    if (!pLoadedEffect)
    {
        bstring effectContents = 0;
        struct bstrList* lines = 0;
        int lineNo = 0;
        {
            FILE* fp = 0;
            bstring effectFile = 0;

            // Decorate the effect name to form the fullpath
            effectFile = bstrcpy(effectName);
            binsert(effectFile, 0, gc->PathPrefix, '?');
            bconcat(effectFile, gc->PathSuffix);

            // Attempt to open the file
            fp = fopen((const char*) effectFile->data, "rb");
            if (!fp)
            {
                bdestroy(gc->ErrorMessage);
                gc->ErrorMessage = bformat("Unable to open effect file '%s'.", effectFile->data);
                bdestroy(effectFile);
                bdestroy(effectKey);
                bstrListDestroy(tokens);
                return 0;
            }

            // Add a new entry to the front of gc->LoadedEffects
            {
                glswList* temp = gc->LoadedEffects;
                gc->LoadedEffects = (glswList*) calloc(sizeof(glswList), 1);
                gc->LoadedEffects->Key = bstrcpy(effectName);
                gc->LoadedEffects->Next = temp;
            }

            // Read in the effect file
            effectContents = bread_gl((bNread) fread, fp);
            fclose(fp);
            bdestroy(effectFile);
            effectFile = 0;
        }

        lines = bsplit(effectContents, '\n');
        bdestroy(effectContents);
        effectContents = 0;

        for (lineNo = 0; lineNo < lines->qty; lineNo++)
        {
            bstring line = lines->entry[lineNo];

            // If the line starts with "--", then it marks a new section
            if (blength(line) >= 2 && line->data[0] == '-' && line->data[1] == '-')
            {
                // Find the first character in [A-Za-z0-9_].
                int colNo = 0;
                for (colNo = 2; colNo < blength(line); colNo++)
                {
                    char c = line->data[colNo];
                    if (__glsw__Alphanumeric(c))
                    {
                        break;
                    }
                }

                // If there's no alphanumeric character,
                // then this marks the start of a new comment block.
                if (colNo >= blength(line))
                {
                    bdestroy(shaderKey);
                    shaderKey = 0;
                }
                else
                {
                    // Keep reading until a non-alphanumeric character is found.
                    int endCol = 0;
                    for (endCol = colNo; endCol < blength(line); endCol++)
                    {
                        char c = line->data[endCol];
                        if (!__glsw__Alphanumeric(c))
                        {
                            break;
                        }
                    }

                    bdestroy(shaderKey);
                    shaderKey = bmidstr(line, colNo, endCol - colNo);

                    // Add a new entry to the shader map.
                    {
                        glswList* temp = gc->ShaderMap;
                        gc->ShaderMap = (glswList*) calloc(sizeof(glswList), 1);
                        gc->ShaderMap->Key = bstrcpy(shaderKey);
                        gc->ShaderMap->Next = temp;
                        gc->ShaderMap->Value = bformat("//__LINE_OFFSET_\n");

                        binsertch(gc->ShaderMap->Key, 0, 1, '.');
                        binsert(gc->ShaderMap->Key, 0, effectName, '?');
                    }

                    // Check for a version mapping.
                    if (gc->TokenMap)
                    {
                        tokens = bsplit(shaderKey, '.');
                        assert(tokens);

                        glswList* pTokenMapping = gc->TokenMap;

                        while (pTokenMapping)
                        {
                            bstring directive = 0;
                            int tokenIndex = 0;

                            // An empty key in the token mapping means "always prepend this directive".
                            // The effect name itself is also checked against the token mapping.
                            if (blength(pTokenMapping->Key) == 0 ||
                                biseq(pTokenMapping->Key, effectName) == 1)
                            {
                                directive = pTokenMapping->Value;
                                binsert(gc->ShaderMap->Value, 0, directive, '?');
                            }

                            // Check all tokens in the current section divider for a mapped token.
                            for (tokenIndex = 0; tokenIndex < tokens->qty && !directive; tokenIndex++)
                            {
                                bstring token = tokens->entry[tokenIndex];
                                if (biseq(pTokenMapping->Key, token) == 1)
                                {
                                    directive = pTokenMapping->Value;
                                    binsert(gc->ShaderMap->Value, 0, directive, '?');
                                }
                            }

                            pTokenMapping = pTokenMapping->Next;
                        }

                        bstrListDestroy(tokens);
                        tokens = 0;
                    }
                }
                continue;
            }
            if (shaderKey)
            {
                bconcat(gc->ShaderMap->Value, line);
                bconchar(gc->ShaderMap->Value, '\n');
            }
        }

        // Cleanup
        bstrListDestroy(lines);
        lines = 0;
        bdestroy(shaderKey);
        shaderKey = 0;
    }

    // Find the longest matching shader key
    pShaderEntry = gc->ShaderMap;

    while (pShaderEntry)
    {
        if (binstr(effectKey, 0, pShaderEntry->Key) == 0 &&
            (!closestMatch || blength(pShaderEntry->Key) > blength(closestMatch->Key)))
        {
            closestMatch = pShaderEntry;
        }

        pShaderEntry = pShaderEntry->Next;
    }

    bstrListDestroy(tokens);
    tokens = 0;
    bdestroy(effectKey);
    effectKey = 0;

    if (!closestMatch)
    {
        bdestroy(gc->ErrorMessage);
        gc->ErrorMessage = bformat("Could not find shader with key '%s'.", pEffectKey);
        return 0;
    }

    return (const char*) closestMatch->Value->data;
}

const char* glswGetError()
{
    glswContext* gc = __glsw__Context;

    if (!gc)
    {
        return "The glsw API has not been initialized.";
    }

    return (const char*) (gc->ErrorMessage ? gc->ErrorMessage->data : 0);
}

int glswAddDirectiveToken(const char* token, const char* directive)
{
    glswContext* gc = __glsw__Context;
    glswList* temp;

    if (!gc)
    {
        return 0;
    }

    temp = gc->TokenMap;
    gc->TokenMap = (glswList*) calloc(sizeof(glswContext), 1);
    if (!gc->TokenMap) {
        return 0;
    }
    gc->TokenMap->Key = bfromcstr(token);
    gc->TokenMap->Value = bfromcstr(directive);
    gc->TokenMap->Next = temp;

    bconchar(gc->TokenMap->Value, '\n');

    return 1;
}

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/glsw/glsw.c

Diff revisions: vs.
Revision Author Commited Message
879 Diff Diff IonutCava picture IonutCava Sun 19 Mar, 2017 19:29:46 +0000

[IonutCava]
- GLSL updates:
— Interface block correction to support tessellation shaders automatically
— Fixed line counting issues with the preprocessor

747 Diff Diff IonutCava picture IonutCava Thu 30 Jun, 2016 15:32:46 +0000

[IonutCava]
- Rework shader recompilation system
— Added a recompile all system for shaders
— Added a recompile selected for materials
— Still buggy

527 Diff Diff IonutCava picture IonutCava Fri 16 Oct, 2015 16:01:06 +0000

[IonutCava]
- EXPERIMENTAL Added Compute based HiZ occlusion culling
- Some fixes to shader line counting system

462 Diff Diff IonutCava picture IonutCava Wed 03 Jun, 2015 15:58:10 +0000

[Ionut]
- More Linux port work:
— Code compiles and links
— Executable launches and:
-— Failes to create a context (running on a virtual machine, so to be expected)
-— Time values are invalid (never debugged linux time code, so to be expected)
Code is now cross-platform Win32 <-> Unix

398 Diff Diff IonutCava picture IonutCava Mon 13 Apr, 2015 16:11:29 +0000

[Ionut]
- Migrated to a pure OpenGL 4.5 implementation
- Fixed a string bug in glsw’s path suffix and prefix strings
- Some profile-guided optimizations

397 Diff Diff IonutCava picture IonutCava Fri 10 Apr, 2015 15:33:01 +0000

[Ionut]
- Disabled VTable initialization for abstract classes on windows
- Fixed an erroneous delegate bind in Kernel for graph plotting

396 Diff Diff IonutCava picture IonutCava Thu 02 Apr, 2015 10:51:16 +0000

[Ionut]
- Small cleanups

381 Diff Diff IonutCava picture IonutCava Fri 20 Mar, 2015 16:54:58 +0000

[Ionut]
- Finished client side batching system.
— Visible nodes are sorted in the same order as the RenderBinItems in the renderbins so draw command order matches preparation order.
— All consecutive nodes that use the same rendering data and state will be rendered using a single multiDrawIndirect command:
— Currently disabled usage of cmd.drawCount() in multidraw command as some OpenGL implementation do not properly update gl_DrawIDARB. Currently using a loop of “drawCount” steps with each invocation of multidraw using a count of 1. To be tested and fixed later.
- Cached attrib locations for GLIM batch data (reducing the number of calls to glGetAttribLocation) when using IM Primitives (bounding boxes, axis arrows, lines, etc)
- Fixed a bug with double-processing of Material shaders

350 Diff Diff IonutCava picture IonutCava Tue 10 Feb, 2015 16:25:39 +0000

[Ionut]
- Simplified some Util-namespace level functions by using STL algorithms where possible
- Formatted the entire codebase using clang-format with the following style: "{BasedOnStyle: Google, IndentWidth: 4}"
- Attempted to make ProfileTimer thread-safe (temp fix until std::chrono works properly in Visual Studio)

337 IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)