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

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

core: refactor the Hash class so that we can hash base types, too.

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