source: trunk/src/gpu/shader.cpp @ 1389

Last change on this file since 1389 was 1389, checked in by sam, 7 years ago

gpu: define the _XBOX macro in our HLSL compilation.

  • Property svn:keywords set to Id
File size: 21.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cmath>
16#include <cstring>
17#include <cstdio>
18
19#ifdef WIN32
20#   define WIN32_LEAN_AND_MEAN
21#   include <windows.h>
22#   if defined USE_D3D9
23#       include <algorithm>
24        using std::min;
25        using std::max;
26#       include <d3d9.h>
27#       include <d3dx9shader.h>
28#   endif
29#endif
30
31#include "core.h"
32#include "lolgl.h"
33
34using namespace std;
35
36#if defined USE_D3D9
37extern IDirect3DDevice9 *g_d3ddevice;
38#elif defined _XBOX
39extern D3DDevice *g_d3ddevice;
40#endif
41
42namespace lol
43{
44
45/*
46 * Shader implementation class
47 */
48
49class ShaderData
50{
51    friend class Shader;
52
53private:
54#if defined USE_D3D9
55    IDirect3DVertexShader9 *vert_shader;
56    IDirect3DPixelShader9 *frag_shader;
57    ID3DXConstantTable *vert_table, *frag_table;
58#elif defined _XBOX
59    D3DVertexShader *vert_shader;
60    D3DPixelShader *frag_shader;
61    ID3DXConstantTable *vert_table, *frag_table;
62#elif !defined __CELLOS_LV2__
63    GLuint prog_id, vert_id, frag_id;
64#else
65    CGprogram vert_id, frag_id;
66#endif
67    uint32_t vert_crc, frag_crc;
68
69    /* Shader patcher */
70    static int GetVersion();
71    static void Patch(char *dst, char const *vert, char const *frag);
72
73    /* Global shader cache */
74    static Shader *shaders[];
75    static int nshaders;
76};
77
78Shader *ShaderData::shaders[256];
79int ShaderData::nshaders = 0;
80
81/*
82 * Public Shader class
83 */
84
85Shader *Shader::Create(char const *lolfx)
86{
87    char *src = new char[strlen(lolfx) + 2];
88    memcpy(src + 1, lolfx, strlen(lolfx) + 1);
89    src[0] = '\n';
90
91    /* Parse the crap */
92    Array<char const *, char const *> sections;
93    char *key = NULL;
94    for (char *parser = src; *parser; )
95    {
96        if (key == NULL && (parser[0] == '\n' || parser[0] == '\r')
97             && parser[1] == '-' && parser[2] == '-' && parser[3] == ' ')
98        {
99            *parser = '\0';
100            parser += 4;
101            key = parser;
102        }
103        else if (key && parser[0] == ' ')
104        {
105            *parser++ = '\0';
106        }
107        else if (key && (parser[0] == '\n' || parser[0] == '\r'))
108        {
109            sections.Push(key, parser);
110            parser++;
111            key = NULL;
112        }
113        else
114        {
115            parser++;
116        }
117    }
118
119    char const *vert = NULL, *frag = NULL;
120    for (int i = 0; i < sections.Count(); i++)
121    {
122#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
123        if (!strcmp(sections[i].m1, "GLSL.Vert"))
124            vert = sections[i].m2;
125        if (!strcmp(sections[i].m1, "GLSL.Frag"))
126            frag = sections[i].m2;
127#else
128        if (!strcmp(sections[i].m1, "HLSL.Vert"))
129            vert = sections[i].m2;
130        if (!strcmp(sections[i].m1, "HLSL.Frag"))
131            frag = sections[i].m2;
132#endif
133    }
134
135    Shader *ret = NULL;
136    if (vert && frag)
137        ret = Create(vert, frag);
138
139    delete[] src;
140
141    return ret;
142}
143
144Shader *Shader::Create(char const *vert, char const *frag)
145{
146    uint32_t new_vert_crc = Hash::Crc32(vert);
147    uint32_t new_frag_crc = Hash::Crc32(frag);
148
149    for (int n = 0; n < ShaderData::nshaders; n++)
150    {
151        if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc
152             && ShaderData::shaders[n]->data->frag_crc == new_frag_crc)
153            return ShaderData::shaders[n];
154    }
155
156    Shader *ret = new Shader(vert, frag);
157    ShaderData::shaders[ShaderData::nshaders] = ret;
158    ShaderData::nshaders++;
159    return ret;
160}
161
162void Shader::Destroy(Shader *shader)
163{
164    /* XXX: do nothing! the shader should remain in cache */
165    (void)shader;
166}
167
168Shader::Shader(char const *vert, char const *frag)
169  : data(new ShaderData())
170{
171#if defined USE_D3D9 || defined _XBOX
172    ID3DXBuffer *shader_code, *error_msg;
173    HRESULT hr;
174    D3DXMACRO macros[] =
175    {
176#if defined _XBOX
177        { "_XBOX", "1" },
178#endif
179        { NULL, NULL }
180    };
181#elif !defined __CELLOS_LV2__
182    char buf[4096], errbuf[4096];
183    char const *shader = buf;
184    GLint status;
185    GLsizei len;
186#else
187    /* Initialise the runtime shader compiler. FIXME: this needs only
188     * to be done once. */
189    cgRTCgcInit();
190#endif
191
192    /* Compile vertex shader */
193    data->vert_crc = Hash::Crc32(vert);
194#if defined USE_D3D9 || defined _XBOX
195    hr = D3DXCompileShader(vert, (UINT)strlen(vert), macros, NULL, "main",
196                           "vs_2_0", 0, &shader_code, &error_msg,
197                           &data->vert_table);
198    if (FAILED(hr))
199    {
200        Log::Error("failed to compile vertex shader: %s",
201                   error_msg ? error_msg->GetBufferPointer() : "error");
202        Log::Error("shader source:\n%s\n", vert);
203    }
204    g_d3ddevice->CreateVertexShader((DWORD *)shader_code->GetBufferPointer(),
205                                    &data->vert_shader);
206    shader_code->Release();
207#elif !defined __CELLOS_LV2__
208    ShaderData::Patch(buf, vert, NULL);
209    data->vert_id = glCreateShader(GL_VERTEX_SHADER);
210    glShaderSource(data->vert_id, 1, &shader, NULL);
211    glCompileShader(data->vert_id);
212
213    glGetShaderiv(data->vert_id, GL_COMPILE_STATUS, &status);
214    if (status != GL_TRUE)
215    {
216        glGetShaderInfoLog(data->vert_id, sizeof(errbuf), &len, errbuf);
217        Log::Error("failed to compile vertex shader: %s", errbuf);
218        Log::Error("shader source:\n%s\n", buf);
219    }
220#else
221    data->vert_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, vert,
222                                    cgGLGetLatestProfile(CG_GL_VERTEX),
223                                    NULL, NULL);
224    if (data->vert_id == NULL)
225    {
226        Log::Error("failed to compile vertex shader");
227        Log::Error("shader source:\n%s\n", vert);
228    }
229#endif
230
231    /* Compile fragment shader */
232    data->frag_crc = Hash::Crc32(frag);
233#if defined USE_D3D9 || defined _XBOX
234    hr = D3DXCompileShader(frag, (UINT)strlen(frag), macros, NULL, "main",
235                           "ps_2_0", 0, &shader_code, &error_msg,
236                           &data->frag_table);
237    if (FAILED(hr))
238    {
239        Log::Error("failed to compile fragment shader: %s",
240                   error_msg ? error_msg->GetBufferPointer() : "error");
241        Log::Error("shader source:\n%s\n", frag);
242    }
243    g_d3ddevice->CreatePixelShader((DWORD *)shader_code->GetBufferPointer(),
244                                   &data->frag_shader);
245    shader_code->Release();
246#elif !defined __CELLOS_LV2__
247    ShaderData::Patch(buf, NULL, frag);
248    data->frag_id = glCreateShader(GL_FRAGMENT_SHADER);
249    glShaderSource(data->frag_id, 1, &shader, NULL);
250    glCompileShader(data->frag_id);
251
252    glGetShaderiv(data->frag_id, GL_COMPILE_STATUS, &status);
253    if (status != GL_TRUE)
254    {
255        glGetShaderInfoLog(data->frag_id, sizeof(errbuf), &len, errbuf);
256        Log::Error("failed to compile fragment shader: %s", errbuf);
257        Log::Error("shader source:\n%s\n", buf);
258    }
259#else
260    data->frag_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, frag,
261                                    cgGLGetLatestProfile(CG_GL_FRAGMENT),
262                                    NULL, NULL);
263    if (data->frag_id == NULL)
264    {
265        Log::Error("failed to compile fragment shader");
266        Log::Error("shader source:\n%s\n", frag);
267    }
268#endif
269
270#if defined USE_D3D9 || defined _XBOX
271    /* FIXME: this is only debug code, we don't need it. */
272    D3DXCONSTANTTABLE_DESC desc;
273    data->frag_table->GetDesc(&desc);
274    for (int i = 0; i < desc.Constants; i++)
275    {
276        D3DXCONSTANT_DESC cdesc;
277        UINT count = 1;
278        D3DXHANDLE h = data->frag_table->GetConstant(NULL, i);
279        data->frag_table->GetConstantDesc(h, &cdesc, &count);
280    }
281    data->vert_table->GetDesc(&desc);
282    for (int i = 0; i < desc.Constants; i++)
283    {
284        D3DXCONSTANT_DESC cdesc;
285        UINT count = 1;
286        D3DXHANDLE h = data->vert_table->GetConstant(NULL, i);
287        data->frag_table->GetConstantDesc(h, &cdesc, &count);
288    }
289#elif !defined __CELLOS_LV2__
290    /* Create program */
291    data->prog_id = glCreateProgram();
292    glAttachShader(data->prog_id, data->vert_id);
293    glAttachShader(data->prog_id, data->frag_id);
294
295    glLinkProgram(data->prog_id);
296    glValidateProgram(data->prog_id);
297#endif
298}
299
300ShaderAttrib Shader::GetAttribLocation(char const *attr,
301                                       VertexUsage usage, int index) const
302{
303    ShaderAttrib ret;
304    ret.m_flags = (uint64_t)(uint16_t)usage << 16;
305    ret.m_flags |= (uint64_t)(uint16_t)index;
306#if defined USE_D3D9 || defined _XBOX
307#elif !defined __CELLOS_LV2__
308    ret.m_flags |= (uint64_t)
309                  (uint32_t)glGetAttribLocation(data->prog_id, attr) << 32;
310#else
311    /* FIXME: can we do this at all on the PS3? */
312#endif
313    return ret;
314}
315
316ShaderUniform Shader::GetUniformLocation(char const *uni) const
317{
318    ShaderUniform ret;
319#if defined USE_D3D9 || defined _XBOX
320    /* Global variables are prefixed with "$" */
321    char tmpname[128];
322    sprintf(tmpname, "$%s", uni);
323    D3DXCONSTANT_DESC cdesc;
324    D3DXHANDLE hr;
325    UINT count;
326
327    count = 0;
328    hr = data->frag_table->GetConstantByName(NULL, tmpname);
329    if (hr)
330        data->frag_table->GetConstantDesc(hr, &cdesc, &count);
331    if (count)
332    {
333        ret.frag = cdesc.RegisterIndex;
334        ret.flags |= 1;
335    }
336
337    count = 0;
338    hr = data->vert_table->GetConstantByName(NULL, tmpname);
339    if (hr)
340        data->vert_table->GetConstantDesc(hr, &cdesc, &count);
341    if (count)
342    {
343        ret.vert = cdesc.RegisterIndex;
344        ret.flags |= 2;
345    }
346#elif !defined __CELLOS_LV2__
347    ret.frag = (uintptr_t)glGetUniformLocation(data->prog_id, uni);
348    ret.vert = 0;
349#else
350    ret.frag = (uintptr_t)cgGetNamedParameter(data->frag_id, uni);
351    ret.vert = (uintptr_t)cgGetNamedParameter(data->vert_id, uni);
352#endif
353    return ret;
354}
355
356void Shader::SetUniform(ShaderUniform const &uni, int i)
357{
358#if defined USE_D3D9 || defined _XBOX
359    SetUniform(uni, ivec4(i, 0, 0, 0));
360#elif !defined __CELLOS_LV2__
361    glUniform1i(uni.frag, i);
362#else
363    /* FIXME: does this exist at all? cgGLSetParameter1i doesn't. */
364#endif
365}
366
367void Shader::SetUniform(ShaderUniform const &uni, ivec2 const &v)
368{
369#if defined USE_D3D9 || defined _XBOX
370    SetUniform(uni, ivec4(v, 0, 0));
371#elif !defined __CELLOS_LV2__
372    glUniform2i(uni.frag, v.x, v.y);
373#else
374    /* FIXME: does this exist at all? */
375#endif
376}
377
378void Shader::SetUniform(ShaderUniform const &uni, ivec3 const &v)
379{
380#if defined USE_D3D9 || defined _XBOX
381    SetUniform(uni, ivec4(v, 0));
382#elif !defined __CELLOS_LV2__
383    glUniform3i(uni.frag, v.x, v.y, v.z);
384#else
385    /* FIXME: does this exist at all? */
386#endif
387}
388
389void Shader::SetUniform(ShaderUniform const &uni, ivec4 const &v)
390{
391#if defined USE_D3D9 || defined _XBOX
392    if (uni.flags & 1)
393        g_d3ddevice->SetPixelShaderConstantI((UINT)uni.frag, &v[0], 1);
394    if (uni.flags & 2)
395        g_d3ddevice->SetVertexShaderConstantI((UINT)uni.vert, &v[0], 1);
396#elif !defined __CELLOS_LV2__
397    glUniform4i(uni.frag, v.x, v.y, v.z, v.w);
398#else
399    /* FIXME: does this exist at all? */
400#endif
401}
402
403void Shader::SetUniform(ShaderUniform const &uni, float f)
404{
405#if defined USE_D3D9 || defined _XBOX
406    SetUniform(uni, vec4(f, 0, 0, 0));
407#elif !defined __CELLOS_LV2__
408    glUniform1f(uni.frag, f);
409#else
410    if (uni.frag)
411        cgGLSetParameter1f((CGparameter)uni.frag, f);
412    if (uni.vert)
413        cgGLSetParameter1f((CGparameter)uni.vert, f);
414#endif
415}
416
417void Shader::SetUniform(ShaderUniform const &uni, vec2 const &v)
418{
419#if defined USE_D3D9 || defined _XBOX
420    SetUniform(uni, vec4(v, 0, 0));
421#elif !defined __CELLOS_LV2__
422    glUniform2fv(uni.frag, 1, &v[0]);
423#else
424    if (uni.frag)
425        cgGLSetParameter2fv((CGparameter)uni.frag, &v[0]);
426    if (uni.vert)
427        cgGLSetParameter2fv((CGparameter)uni.vert, &v[0]);
428#endif
429}
430
431void Shader::SetUniform(ShaderUniform const &uni, vec3 const &v)
432{
433#if defined USE_D3D9 || defined _XBOX
434    SetUniform(uni, vec4(v, 0));
435#elif !defined __CELLOS_LV2__
436    glUniform3fv(uni.frag, 1, &v[0]);
437#else
438    if (uni.frag)
439        cgGLSetParameter3fv((CGparameter)uni.frag, &v[0]);
440    if (uni.vert)
441        cgGLSetParameter3fv((CGparameter)uni.vert, &v[0]);
442#endif
443}
444
445void Shader::SetUniform(ShaderUniform const &uni, vec4 const &v)
446{
447#if defined USE_D3D9 || defined _XBOX
448    if (uni.flags & 1)
449        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &v[0], 1);
450    if (uni.flags & 2)
451        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &v[0], 1);
452#elif !defined __CELLOS_LV2__
453    glUniform4fv(uni.frag, 1, &v[0]);
454#else
455    if (uni.frag)
456        cgGLSetParameter4fv((CGparameter)uni.frag, &v[0]);
457    if (uni.vert)
458        cgGLSetParameter4fv((CGparameter)uni.vert, &v[0]);
459#endif
460}
461
462void Shader::SetUniform(ShaderUniform const &uni, mat2 const &m)
463{
464#if defined USE_D3D9 || defined _XBOX
465    /* FIXME: do we need padding here like for the mat3 version? */
466    if (uni.flags & 1)
467        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 1);
468    if (uni.flags & 2)
469        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 1);
470#elif !defined __CELLOS_LV2__
471    glUniformMatrix2fv(uni.frag, 1, GL_FALSE, &m[0][0]);
472#else
473    mat4 tmp(m, 1.0f, 1.0f);
474    if (uni.frag)
475        cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]);
476    if (uni.vert)
477        cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]);
478#endif
479}
480
481void Shader::SetUniform(ShaderUniform const &uni, mat3 const &m)
482{
483#if defined USE_D3D9 || defined _XBOX
484    /* Padding matrix columns is necessary on DirectX. We need to create
485     * a new data structure; a 4×4 matrix will do. */
486    mat4 tmp(m, 1.0f);
487    if (uni.flags & 1)
488        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &tmp[0][0], 3);
489    if (uni.flags & 2)
490        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &tmp[0][0], 3);
491#elif !defined __CELLOS_LV2__
492    glUniformMatrix3fv(uni.frag, 1, GL_FALSE, &m[0][0]);
493#else
494    /* FIXME: check it's the proper way to do this */
495    mat4 tmp(m, 1.0f);
496    if (uni.frag)
497        cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]);
498    if (uni.vert)
499        cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]);
500#endif
501}
502
503void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m)
504{
505#if defined USE_D3D9 || defined _XBOX
506    if (uni.flags & 1)
507        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 4);
508    if (uni.flags & 2)
509        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 4);
510#elif !defined __CELLOS_LV2__
511    glUniformMatrix4fv(uni.frag, 1, GL_FALSE, &m[0][0]);
512#else
513    if (uni.frag)
514        cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]);
515    if (uni.vert)
516        cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]);
517#endif
518}
519
520void Shader::Bind() const
521{
522#if defined USE_D3D9 || defined _XBOX
523    HRESULT hr;
524    hr = g_d3ddevice->SetVertexShader(data->vert_shader);
525    hr = g_d3ddevice->SetPixelShader(data->frag_shader);
526#elif !defined __CELLOS_LV2__
527    glUseProgram(data->prog_id);
528#else
529    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
530    cgGLBindProgram(data->vert_id);
531    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
532    cgGLBindProgram(data->frag_id);
533#endif
534}
535
536void Shader::Unbind() const
537{
538#if defined USE_D3D9 || defined _XBOX
539    HRESULT hr;
540    hr = g_d3ddevice->SetVertexShader(NULL);
541    hr = g_d3ddevice->SetPixelShader(NULL);
542#elif !defined __CELLOS_LV2__
543    /* FIXME: untested */
544    glUseProgram(0);
545#else
546    /* FIXME: untested */
547    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
548    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
549#endif
550}
551
552Shader::~Shader()
553{
554#if defined USE_D3D9 || defined _XBOX
555    data->vert_shader->Release();
556    data->vert_table->Release();
557    data->frag_shader->Release();
558    data->frag_table->Release();
559#elif !defined __CELLOS_LV2__
560    glDetachShader(data->prog_id, data->vert_id);
561    glDetachShader(data->prog_id, data->frag_id);
562    glDeleteShader(data->vert_id);
563    glDeleteShader(data->frag_id);
564    glDeleteProgram(data->prog_id);
565#else
566    cgDestroyProgram(data->vert_id);
567    cgDestroyProgram(data->frag_id);
568#endif
569    delete data;
570}
571
572/* Try to detect shader compiler features */
573int ShaderData::GetVersion()
574{
575    static int version = 0;
576
577#if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__
578    if (!version)
579    {
580        char buf[4096];
581        GLsizei len;
582
583        int id = glCreateShader(GL_VERTEX_SHADER);
584
585        /* Can we compile 1.30 shaders? */
586        char const *test130 =
587            "#version 130\n"
588            "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
589        glShaderSource(id, 1, &test130, NULL);
590        glCompileShader(id);
591        glGetShaderInfoLog(id, sizeof(buf), &len, buf);
592        if (len <= 0)
593            version = 130;
594
595        /* If not, can we compile 1.20 shaders? */
596        if (!version)
597        {
598            char const *test120 =
599                "#version 120\n"
600                "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
601            glShaderSource(id, 1, &test120, NULL);
602            glCompileShader(id);
603            glGetShaderInfoLog(id, sizeof(buf), &len, buf);
604            if (len <= 0)
605                version = 120;
606        }
607
608        /* Otherwise, assume we can compile 1.10 shaders. */
609        if (!version)
610            version = 110;
611
612        glDeleteShader(id);
613    }
614#endif
615
616    return version;
617}
618
619/* Simple shader source patching for old GLSL versions.
620 * If supported version is 1.30, do nothing.
621 * If supported version is 1.20:
622 *  - replace "#version 130" with "#version 120"
623 */
624void ShaderData::Patch(char *dst, char const *vert, char const *frag)
625{
626    int ver_driver = GetVersion();
627
628    strcpy(dst, vert ? vert : frag);
629    if (ver_driver >= 130)
630        return;
631
632    int ver_shader = 110;
633    char *parser = strstr(dst, "#version");
634    if (parser)
635        ver_shader = atoi(parser + strlen("#version"));
636
637    if (ver_shader > 120 && ver_driver <= 120)
638    {
639        char const *end = dst + strlen(dst) + 1;
640
641        /* Find main() */
642        parser = strstr(dst, "main");
643        if (!parser) return;
644        parser = strstr(parser, "(");
645        if (!parser) return;
646        parser = strstr(parser, ")");
647        if (!parser) return;
648        parser = strstr(parser, "{");
649        if (!parser) return;
650        char *main = parser + 1;
651
652        /* Perform main() replaces */
653        char const * const main_replaces[] =
654        {
655#if 0
656            "in vec2 in_Vertex;", "vec2 in_Vertex = gl_Vertex.xy;",
657            "in vec3 in_Vertex;", "vec3 in_Vertex = gl_Vertex.xyz;",
658            "in vec4 in_Vertex;", "vec4 in_Vertex = gl_Vertex.xyzw;",
659
660            "in vec2 in_Color;", "vec2 in_Color = gl_Color.xy;",
661            "in vec3 in_Color;", "vec3 in_Color = gl_Color.xyz;",
662            "in vec4 in_Color;", "vec4 in_Color = gl_Color.xyzw;",
663
664            "in vec2 in_MultiTexCoord0;",
665               "vec2 in_MultiTexCoord0 = gl_MultiTexCoord0.xy;",
666            "in vec2 in_MultiTexCoord1;",
667               "vec2 in_MultiTexCoord1 = gl_MultiTexCoord1.xy;",
668            "in vec2 in_MultiTexCoord2;",
669               "vec2 in_MultiTexCoord2 = gl_MultiTexCoord2.xy;",
670            "in vec2 in_MultiTexCoord3;",
671               "vec2 in_MultiTexCoord3 = gl_MultiTexCoord3.xy;",
672            "in vec2 in_MultiTexCoord4;",
673               "vec2 in_MultiTexCoord4 = gl_MultiTexCoord4.xy;",
674            "in vec2 in_MultiTexCoord5;",
675               "vec2 in_MultiTexCoord5 = gl_MultiTexCoord5.xy;",
676            "in vec2 in_MultiTexCoord6;",
677               "vec2 in_MultiTexCoord6 = gl_MultiTexCoord6.xy;",
678            "in vec2 in_MultiTexCoord7;",
679               "vec2 in_MultiTexCoord7 = gl_MultiTexCoord7.xy;",
680#endif
681
682            NULL
683        };
684
685        for (char const * const *rep = main_replaces; rep[0]; rep += 2)
686        {
687            char *match = strstr(dst, rep[0]);
688            if (match && match < main)
689            {
690                size_t l0 = strlen(rep[0]);
691                size_t l1 = strlen(rep[1]);
692                memmove(main + l1, main, end - main);
693                memcpy(main, rep[1], l1);
694                memset(match, ' ', l0);
695                main += l1;
696                end += l1;
697            }
698        }
699
700        /* Perform small replaces */
701        char const * const fast_replaces[] =
702        {
703            "#version 130", "#version 120",
704            "in vec2", vert ? "attribute vec2" : "varying vec2",
705            "in vec3", vert ? "attribute vec3" : "varying vec3",
706            "in vec4", vert ? "attribute vec4" : "varying vec4",
707            "in mat4", vert ? "attribute mat4" : "varying mat4",
708            "out vec2", "varying vec2",
709            "out vec3", "varying vec3",
710            "out vec4", "varying vec4",
711            "out mat4", "varying mat4",
712            NULL
713        };
714
715        for (char const * const *rep = fast_replaces; rep[0]; rep += 2)
716        {
717            char *match;
718            while ((match = strstr(dst, rep[0])))
719            {
720                size_t l0 = strlen(rep[0]);
721                size_t l1 = strlen(rep[1]);
722
723                if (l1 > l0)
724                    memmove(match + l1, match + l0, (end - match) - l0);
725                memcpy(match, rep[1], l1);
726                if (l1 < l0)
727                    memset(match + l0, ' ', l1 - l0);
728                end += l1 - l0;
729            }
730        }
731    }
732}
733
734} /* namespace lol */
735
Note: See TracBrowser for help on using the repository browser.