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

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

gpu: first shot at a Shader::SetTexture() function.

  • Property svn:keywords set to Id
File size: 21.6 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::SetTexture(ShaderUniform const &uni, int id, int index)
521{
522    if (id == -1)
523        return;
524#if defined USE_D3D9 || defined _XBOX
525    /* FIXME: unimplemented */
526#elif !defined __CELLOS_LV2__
527    glActiveTexture(GL_TEXTURE0 + index);
528    //glEnable(GL_TEXTURE_2D);
529    glBindTexture(GL_TEXTURE_2D, id);
530    SetUniform(uni, index);
531#else
532    /* FIXME: unimplemented */
533#endif
534}
535
536void Shader::Bind() const
537{
538#if defined USE_D3D9 || defined _XBOX
539    HRESULT hr;
540    hr = g_d3ddevice->SetVertexShader(data->vert_shader);
541    hr = g_d3ddevice->SetPixelShader(data->frag_shader);
542#elif !defined __CELLOS_LV2__
543    glUseProgram(data->prog_id);
544#else
545    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
546    cgGLBindProgram(data->vert_id);
547    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
548    cgGLBindProgram(data->frag_id);
549#endif
550}
551
552void Shader::Unbind() const
553{
554#if defined USE_D3D9 || defined _XBOX
555    HRESULT hr;
556    hr = g_d3ddevice->SetVertexShader(NULL);
557    hr = g_d3ddevice->SetPixelShader(NULL);
558#elif !defined __CELLOS_LV2__
559    /* FIXME: untested */
560    glUseProgram(0);
561#else
562    /* FIXME: untested */
563    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
564    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
565#endif
566}
567
568Shader::~Shader()
569{
570#if defined USE_D3D9 || defined _XBOX
571    data->vert_shader->Release();
572    data->vert_table->Release();
573    data->frag_shader->Release();
574    data->frag_table->Release();
575#elif !defined __CELLOS_LV2__
576    glDetachShader(data->prog_id, data->vert_id);
577    glDetachShader(data->prog_id, data->frag_id);
578    glDeleteShader(data->vert_id);
579    glDeleteShader(data->frag_id);
580    glDeleteProgram(data->prog_id);
581#else
582    cgDestroyProgram(data->vert_id);
583    cgDestroyProgram(data->frag_id);
584#endif
585    delete data;
586}
587
588/* Try to detect shader compiler features */
589int ShaderData::GetVersion()
590{
591    static int version = 0;
592
593#if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__
594    if (!version)
595    {
596        char buf[4096];
597        GLsizei len;
598
599        int id = glCreateShader(GL_VERTEX_SHADER);
600
601        /* Can we compile 1.30 shaders? */
602        char const *test130 =
603            "#version 130\n"
604            "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
605        glShaderSource(id, 1, &test130, NULL);
606        glCompileShader(id);
607        glGetShaderInfoLog(id, sizeof(buf), &len, buf);
608        if (len <= 0)
609            version = 130;
610
611        /* If not, can we compile 1.20 shaders? */
612        if (!version)
613        {
614            char const *test120 =
615                "#version 120\n"
616                "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
617            glShaderSource(id, 1, &test120, NULL);
618            glCompileShader(id);
619            glGetShaderInfoLog(id, sizeof(buf), &len, buf);
620            if (len <= 0)
621                version = 120;
622        }
623
624        /* Otherwise, assume we can compile 1.10 shaders. */
625        if (!version)
626            version = 110;
627
628        glDeleteShader(id);
629    }
630#endif
631
632    return version;
633}
634
635/* Simple shader source patching for old GLSL versions.
636 * If supported version is 1.30, do nothing.
637 * If supported version is 1.20:
638 *  - replace "#version 130" with "#version 120"
639 */
640void ShaderData::Patch(char *dst, char const *vert, char const *frag)
641{
642    int ver_driver = GetVersion();
643
644    strcpy(dst, vert ? vert : frag);
645    if (ver_driver >= 130)
646        return;
647
648    int ver_shader = 110;
649    char *parser = strstr(dst, "#version");
650    if (parser)
651        ver_shader = atoi(parser + strlen("#version"));
652
653    if (ver_shader > 120 && ver_driver <= 120)
654    {
655        char const *end = dst + strlen(dst) + 1;
656
657        /* Find main() */
658        parser = strstr(dst, "main");
659        if (!parser) return;
660        parser = strstr(parser, "(");
661        if (!parser) return;
662        parser = strstr(parser, ")");
663        if (!parser) return;
664        parser = strstr(parser, "{");
665        if (!parser) return;
666        char *main = parser + 1;
667
668        /* Perform main() replaces */
669        char const * const main_replaces[] =
670        {
671#if 0
672            "in vec2 in_Vertex;", "vec2 in_Vertex = gl_Vertex.xy;",
673            "in vec3 in_Vertex;", "vec3 in_Vertex = gl_Vertex.xyz;",
674            "in vec4 in_Vertex;", "vec4 in_Vertex = gl_Vertex.xyzw;",
675
676            "in vec2 in_Color;", "vec2 in_Color = gl_Color.xy;",
677            "in vec3 in_Color;", "vec3 in_Color = gl_Color.xyz;",
678            "in vec4 in_Color;", "vec4 in_Color = gl_Color.xyzw;",
679
680            "in vec2 in_MultiTexCoord0;",
681               "vec2 in_MultiTexCoord0 = gl_MultiTexCoord0.xy;",
682            "in vec2 in_MultiTexCoord1;",
683               "vec2 in_MultiTexCoord1 = gl_MultiTexCoord1.xy;",
684            "in vec2 in_MultiTexCoord2;",
685               "vec2 in_MultiTexCoord2 = gl_MultiTexCoord2.xy;",
686            "in vec2 in_MultiTexCoord3;",
687               "vec2 in_MultiTexCoord3 = gl_MultiTexCoord3.xy;",
688            "in vec2 in_MultiTexCoord4;",
689               "vec2 in_MultiTexCoord4 = gl_MultiTexCoord4.xy;",
690            "in vec2 in_MultiTexCoord5;",
691               "vec2 in_MultiTexCoord5 = gl_MultiTexCoord5.xy;",
692            "in vec2 in_MultiTexCoord6;",
693               "vec2 in_MultiTexCoord6 = gl_MultiTexCoord6.xy;",
694            "in vec2 in_MultiTexCoord7;",
695               "vec2 in_MultiTexCoord7 = gl_MultiTexCoord7.xy;",
696#endif
697
698            NULL
699        };
700
701        for (char const * const *rep = main_replaces; rep[0]; rep += 2)
702        {
703            char *match = strstr(dst, rep[0]);
704            if (match && match < main)
705            {
706                size_t l0 = strlen(rep[0]);
707                size_t l1 = strlen(rep[1]);
708                memmove(main + l1, main, end - main);
709                memcpy(main, rep[1], l1);
710                memset(match, ' ', l0);
711                main += l1;
712                end += l1;
713            }
714        }
715
716        /* Perform small replaces */
717        char const * const fast_replaces[] =
718        {
719            "#version 130", "#version 120",
720            "in vec2", vert ? "attribute vec2" : "varying vec2",
721            "in vec3", vert ? "attribute vec3" : "varying vec3",
722            "in vec4", vert ? "attribute vec4" : "varying vec4",
723            "in mat4", vert ? "attribute mat4" : "varying mat4",
724            "out vec2", "varying vec2",
725            "out vec3", "varying vec3",
726            "out vec4", "varying vec4",
727            "out mat4", "varying mat4",
728            NULL
729        };
730
731        for (char const * const *rep = fast_replaces; rep[0]; rep += 2)
732        {
733            char *match;
734            while ((match = strstr(dst, rep[0])))
735            {
736                size_t l0 = strlen(rep[0]);
737                size_t l1 = strlen(rep[1]);
738
739                if (l1 > l0)
740                    memmove(match + l1, match + l0, (end - match) - l0);
741                memcpy(match, rep[1], l1);
742                if (l1 < l0)
743                    memset(match + l0, ' ', l1 - l0);
744                end += l1 - l0;
745            }
746        }
747    }
748}
749
750} /* namespace lol */
751
Note: See TracBrowser for help on using the repository browser.