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

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

gpu: warn when a vertex shader attribute isn’t found and do not crash
when a stream is not found in the vertex declaration at bind time.

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