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

Last change on this file since 1228 was 1228, checked in by sam, 10 years ago

gpu: port the vertex buffer abstraction layer to OpenGL.

  • Property svn:keywords set to Id
File size: 17.9 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 *vert, char const *frag)
86{
87    uint32_t new_vert_crc = Hash::Crc32(vert);
88    uint32_t new_frag_crc = Hash::Crc32(frag);
89
90    for (int n = 0; n < ShaderData::nshaders; n++)
91    {
92        if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc
93             && ShaderData::shaders[n]->data->frag_crc == new_frag_crc)
94            return ShaderData::shaders[n];
95    }
96
97    Shader *ret = new Shader(vert, frag);
98    ShaderData::shaders[ShaderData::nshaders] = ret;
99    ShaderData::nshaders++;
100    return ret;
101}
102
103void Shader::Destroy(Shader *shader)
104{
105    /* XXX: do nothing! the shader should remain in cache */
106    (void)shader;
107}
108
109Shader::Shader(char const *vert, char const *frag)
110  : data(new ShaderData())
111{
112#if defined USE_D3D9 || defined _XBOX
113    ID3DXBuffer *shader_code, *error_msg;
114    HRESULT hr;
115#elif !defined __CELLOS_LV2__
116    char buf[4096], errbuf[4096];
117    char const *shader = buf;
118    GLint status;
119    GLsizei len;
120#else
121    /* Initialise the runtime shader compiler. FIXME: this needs only
122     * to be done once. */
123    cgRTCgcInit();
124#endif
125
126    /* Compile vertex shader */
127    data->vert_crc = Hash::Crc32(vert);
128#if defined USE_D3D9 || defined _XBOX
129    hr = D3DXCompileShader(vert, (UINT)strlen(vert), NULL, NULL, "main",
130                           "vs_2_0", 0, &shader_code, &error_msg,
131                           &data->vert_table);
132    if (FAILED(hr))
133    {
134        Log::Error("failed to compile vertex shader: %s",
135                   error_msg ? error_msg->GetBufferPointer() : "error");
136        Log::Error("shader source:\n%s\n", vert);
137    }
138    g_d3ddevice->CreateVertexShader((DWORD *)shader_code->GetBufferPointer(),
139                                    &data->vert_shader);
140    shader_code->Release();
141#elif !defined __CELLOS_LV2__
142    ShaderData::Patch(buf, vert, NULL);
143    data->vert_id = glCreateShader(GL_VERTEX_SHADER);
144    glShaderSource(data->vert_id, 1, &shader, NULL);
145    glCompileShader(data->vert_id);
146
147    glGetShaderiv(data->vert_id, GL_COMPILE_STATUS, &status);
148    if (status != GL_TRUE)
149    {
150        glGetShaderInfoLog(data->vert_id, sizeof(errbuf), &len, errbuf);
151        Log::Error("failed to compile vertex shader: %s", errbuf);
152        Log::Error("shader source:\n%s\n", buf);
153    }
154#else
155    data->vert_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, vert,
156                                    cgGLGetLatestProfile(CG_GL_VERTEX),
157                                    NULL, NULL);
158    if (data->vert_id == NULL)
159    {
160        Log::Error("failed to compile vertex shader");
161        Log::Error("shader source:\n%s\n", vert);
162    }
163#endif
164
165    /* Compile fragment shader */
166    data->frag_crc = Hash::Crc32(frag);
167#if defined USE_D3D9 || defined _XBOX
168    hr = D3DXCompileShader(frag, (UINT)strlen(frag), NULL, NULL, "main",
169                           "ps_2_0", 0, &shader_code, &error_msg,
170                           &data->frag_table);
171    if (FAILED(hr))
172    {
173        Log::Error("failed to compile fragment shader: %s",
174                   error_msg ? error_msg->GetBufferPointer() : "error");
175        Log::Error("shader source:\n%s\n", frag);
176    }
177    g_d3ddevice->CreatePixelShader((DWORD *)shader_code->GetBufferPointer(),
178                                   &data->frag_shader);
179    shader_code->Release();
180#elif !defined __CELLOS_LV2__
181    ShaderData::Patch(buf, NULL, frag);
182    data->frag_id = glCreateShader(GL_FRAGMENT_SHADER);
183    glShaderSource(data->frag_id, 1, &shader, NULL);
184    glCompileShader(data->frag_id);
185
186    glGetShaderiv(data->frag_id, GL_COMPILE_STATUS, &status);
187    if (status != GL_TRUE)
188    {
189        glGetShaderInfoLog(data->frag_id, sizeof(errbuf), &len, errbuf);
190        Log::Error("failed to compile fragment shader: %s", errbuf);
191        Log::Error("shader source:\n%s\n", buf);
192    }
193#else
194    data->frag_id = cgCreateProgram(cgCreateContext(), CG_SOURCE, frag,
195                                    cgGLGetLatestProfile(CG_GL_FRAGMENT),
196                                    NULL, NULL);
197    if (data->frag_id == NULL)
198    {
199        Log::Error("failed to compile fragment shader");
200        Log::Error("shader source:\n%s\n", frag);
201    }
202#endif
203
204#if defined USE_D3D9 || defined _XBOX
205    /* FIXME: this is only debug code, we don't need it. */
206    D3DXCONSTANTTABLE_DESC desc;
207    data->frag_table->GetDesc(&desc);
208    for (int i = 0; i < desc.Constants; i++)
209    {
210        D3DXCONSTANT_DESC cdesc;
211        UINT count = 1;
212        D3DXHANDLE h = data->frag_table->GetConstant(NULL, i);
213        data->frag_table->GetConstantDesc(h, &cdesc, &count);
214    }
215    data->vert_table->GetDesc(&desc);
216    for (int i = 0; i < desc.Constants; i++)
217    {
218        D3DXCONSTANT_DESC cdesc;
219        UINT count = 1;
220        D3DXHANDLE h = data->vert_table->GetConstant(NULL, i);
221        data->frag_table->GetConstantDesc(h, &cdesc, &count);
222    }
223#elif !defined __CELLOS_LV2__
224    /* Create program */
225    data->prog_id = glCreateProgram();
226    glAttachShader(data->prog_id, data->vert_id);
227    glAttachShader(data->prog_id, data->frag_id);
228
229    glLinkProgram(data->prog_id);
230    glValidateProgram(data->prog_id);
231#endif
232}
233
234ShaderAttrib Shader::GetAttribLocation(char const *attr,
235                                       VertexUsage usage, int index) const
236{
237    ShaderAttrib ret;
238    ret.m_flags = (uint64_t)(uint16_t)usage << 16;
239    ret.m_flags |= (uint64_t)(uint16_t)index;
240#if defined USE_D3D9 || defined _XBOX
241#elif !defined __CELLOS_LV2__
242    ret.m_flags |= (uint64_t)
243                  (uint32_t)glGetAttribLocation(data->prog_id, attr) << 32;
244#else
245    /* FIXME: can we do this at all on the PS3? */
246#endif
247    return ret;
248}
249
250ShaderUniform Shader::GetUniformLocation(char const *uni) const
251{
252    ShaderUniform ret;
253#if defined USE_D3D9 || defined _XBOX
254    /* Global variables are prefixed with "$" */
255    char tmpname[128];
256    sprintf(tmpname, "$%s", uni);
257    D3DXCONSTANT_DESC cdesc;
258    D3DXHANDLE hr;
259    UINT count;
260
261    count = 0;
262    hr = data->frag_table->GetConstantByName(NULL, tmpname);
263    if (hr)
264        data->frag_table->GetConstantDesc(hr, &cdesc, &count);
265    if (count)
266    {
267        ret.frag = cdesc.RegisterIndex;
268        ret.flags |= 1;
269    }
270
271    count = 0;
272    hr = data->vert_table->GetConstantByName(NULL, tmpname);
273    if (hr)
274        data->vert_table->GetConstantDesc(hr, &cdesc, &count);
275    if (count)
276    {
277        ret.vert = cdesc.RegisterIndex;
278        ret.flags |= 2;
279    }
280#elif !defined __CELLOS_LV2__
281    ret.frag = (uintptr_t)glGetUniformLocation(data->prog_id, uni);
282    ret.vert = 0;
283#else
284    ret.frag = (uintptr_t)cgGetNamedParameter(data->frag_id, uni);
285    ret.vert = (uintptr_t)cgGetNamedParameter(data->vert_id, uni);
286#endif
287    return ret;
288}
289
290void Shader::SetUniform(ShaderUniform const &uni, int i)
291{
292#if defined USE_D3D9 || defined _XBOX
293    SetUniform(uni, ivec4(i, 0, 0, 0));
294#elif !defined __CELLOS_LV2__
295    glUniform1i(uni.frag, i);
296#else
297    /* FIXME: does this exist at all? */
298    //cgGLSetParameter1i((CGparameter)uni.frag, i);
299#endif
300}
301
302void Shader::SetUniform(ShaderUniform const &uni, ivec2 const &v)
303{
304#if defined USE_D3D9 || defined _XBOX
305    SetUniform(uni, ivec4(v, 0, 0));
306#elif !defined __CELLOS_LV2__
307    glUniform2i(uni.frag, v.x, v.y);
308#else
309    /* FIXME: does this exist at all? */
310#endif
311}
312
313void Shader::SetUniform(ShaderUniform const &uni, ivec3 const &v)
314{
315#if defined USE_D3D9 || defined _XBOX
316    SetUniform(uni, ivec4(v, 0));
317#elif !defined __CELLOS_LV2__
318    glUniform3i(uni.frag, v.x, v.y, v.z);
319#else
320    /* FIXME: does this exist at all? */
321#endif
322}
323
324void Shader::SetUniform(ShaderUniform const &uni, ivec4 const &v)
325{
326#if defined USE_D3D9 || defined _XBOX
327    if (uni.flags & 1)
328        g_d3ddevice->SetPixelShaderConstantI((UINT)uni.frag, &v[0], 1);
329    if (uni.flags & 2)
330        g_d3ddevice->SetVertexShaderConstantI((UINT)uni.vert, &v[0], 1);
331#elif !defined __CELLOS_LV2__
332    glUniform4i(uni.frag, v.x, v.y, v.z, v.w);
333#else
334    /* FIXME: does this exist at all? */
335#endif
336}
337
338void Shader::SetUniform(ShaderUniform const &uni, float f)
339{
340#if defined USE_D3D9 || defined _XBOX
341    SetUniform(uni, vec4(f, 0, 0, 0));
342#elif !defined __CELLOS_LV2__
343    glUniform1f(uni.frag, f);
344#else
345    if (uni.frag)
346        cgGLSetParameter1f((CGparameter)uni.frag, f);
347    if (uni.vert)
348        cgGLSetParameter1f((CGparameter)uni.vert, f);
349#endif
350}
351
352void Shader::SetUniform(ShaderUniform const &uni, vec2 const &v)
353{
354#if defined USE_D3D9 || defined _XBOX
355    SetUniform(uni, vec4(v, 0, 0));
356#elif !defined __CELLOS_LV2__
357    glUniform2f(uni.frag, v.x, v.y);
358#else
359    if (uni.frag)
360        cgGLSetParameter2f((CGparameter)uni.frag, v.x, v.y);
361    if (uni.vert)
362        cgGLSetParameter2f((CGparameter)uni.vert, v.x, v.y);
363#endif
364}
365
366void Shader::SetUniform(ShaderUniform const &uni, vec3 const &v)
367{
368#if defined USE_D3D9 || defined _XBOX
369    SetUniform(uni, vec4(v, 0));
370#elif !defined __CELLOS_LV2__
371    glUniform3f(uni.frag, v.x, v.y, v.z);
372#else
373    if (uni.frag)
374        cgGLSetParameter3f((CGparameter)uni.frag, v.x, v.y, v.z);
375    if (uni.vert)
376        cgGLSetParameter3f((CGparameter)uni.vert, v.x, v.y, v.z);
377#endif
378}
379
380void Shader::SetUniform(ShaderUniform const &uni, vec4 const &v)
381{
382#if defined USE_D3D9 || defined _XBOX
383    if (uni.flags & 1)
384        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &v[0], 1);
385    if (uni.flags & 2)
386        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &v[0], 1);
387#elif !defined __CELLOS_LV2__
388    glUniform4f(uni.frag, v.x, v.y, v.z, v.w);
389#else
390    /* FIXME: use the array versions of these functions */
391    if (uni.frag)
392        cgGLSetParameter4f((CGparameter)uni.frag, v.x, v.y, v.z, v.w);
393    if (uni.vert)
394        cgGLSetParameter4f((CGparameter)uni.vert, v.x, v.y, v.z, v.w);
395#endif
396}
397
398void Shader::SetUniform(ShaderUniform const &uni, mat4 const &m)
399{
400#if defined USE_D3D9 || defined _XBOX
401    if (uni.flags & 1)
402        g_d3ddevice->SetPixelShaderConstantF((UINT)uni.frag, &m[0][0], 4);
403    if (uni.flags & 2)
404        g_d3ddevice->SetVertexShaderConstantF((UINT)uni.vert, &m[0][0], 4);
405#elif !defined __CELLOS_LV2__
406    glUniformMatrix4fv(uni.frag, 1, GL_FALSE, &m[0][0]);
407#else
408    if (uni.frag)
409        cgGLSetMatrixParameterfc((CGparameter)uni.frag, &m[0][0]);
410    if (uni.vert)
411        cgGLSetMatrixParameterfc((CGparameter)uni.vert, &m[0][0]);
412#endif
413}
414
415void Shader::Bind() const
416{
417#if defined USE_D3D9 || defined _XBOX
418    HRESULT hr;
419    hr = g_d3ddevice->SetVertexShader(data->vert_shader);
420    hr = g_d3ddevice->SetPixelShader(data->frag_shader);
421#elif !defined __CELLOS_LV2__
422    glUseProgram(data->prog_id);
423#else
424    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
425    cgGLBindProgram(data->vert_id);
426    cgGLEnableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
427    cgGLBindProgram(data->frag_id);
428#endif
429}
430
431Shader::~Shader()
432{
433#if defined USE_D3D9 || defined _XBOX
434    data->vert_shader->Release();
435    data->vert_table->Release();
436    data->frag_shader->Release();
437    data->frag_table->Release();
438#elif !defined __CELLOS_LV2__
439    glDetachShader(data->prog_id, data->vert_id);
440    glDetachShader(data->prog_id, data->frag_id);
441    glDeleteShader(data->vert_id);
442    glDeleteShader(data->frag_id);
443    glDeleteProgram(data->prog_id);
444#else
445    cgDestroyProgram(data->vert_id);
446    cgDestroyProgram(data->frag_id);
447#endif
448    delete data;
449}
450
451/* Try to detect shader compiler features */
452int ShaderData::GetVersion()
453{
454    static int version = 0;
455
456#if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__
457    if (!version)
458    {
459        char buf[4096];
460        GLsizei len;
461
462        int id = glCreateShader(GL_VERTEX_SHADER);
463
464        /* Can we compile 1.30 shaders? */
465        char const *test130 =
466            "#version 130\n"
467            "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
468        glShaderSource(id, 1, &test130, NULL);
469        glCompileShader(id);
470        glGetShaderInfoLog(id, sizeof(buf), &len, buf);
471        if (len <= 0)
472            version = 130;
473
474        /* If not, can we compile 1.20 shaders? */
475        if (!version)
476        {
477            char const *test120 =
478                "#version 120\n"
479                "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
480            glShaderSource(id, 1, &test120, NULL);
481            glCompileShader(id);
482            glGetShaderInfoLog(id, sizeof(buf), &len, buf);
483            if (len <= 0)
484                version = 120;
485        }
486
487        /* Otherwise, assume we can compile 1.10 shaders. */
488        if (!version)
489            version = 110;
490
491        glDeleteShader(id);
492    }
493#endif
494
495    return version;
496}
497
498/* Simple shader source patching for old GLSL versions.
499 * If supported version is 1.30, do nothing.
500 * If supported version is 1.20:
501 *  - replace "#version 130" with "#version 120"
502 */
503void ShaderData::Patch(char *dst, char const *vert, char const *frag)
504{
505    int ver_driver = GetVersion();
506
507    strcpy(dst, vert ? vert : frag);
508    if (ver_driver >= 130)
509        return;
510
511    int ver_shader = 110;
512    char *parser = strstr(dst, "#version");
513    if (parser)
514        ver_shader = atoi(parser + strlen("#version"));
515
516    if (ver_shader > 120 && ver_driver <= 120)
517    {
518        char const *end = dst + strlen(dst) + 1;
519
520        /* Find main() */
521        parser = strstr(dst, "main");
522        if (!parser) return;
523        parser = strstr(parser, "(");
524        if (!parser) return;
525        parser = strstr(parser, ")");
526        if (!parser) return;
527        parser = strstr(parser, "{");
528        if (!parser) return;
529        char *main = parser + 1;
530
531        /* Perform main() replaces */
532        char const * const main_replaces[] =
533        {
534#if 0
535            "in vec2 in_Vertex;", "vec2 in_Vertex = gl_Vertex.xy;",
536            "in vec3 in_Vertex;", "vec3 in_Vertex = gl_Vertex.xyz;",
537            "in vec4 in_Vertex;", "vec4 in_Vertex = gl_Vertex.xyzw;",
538
539            "in vec2 in_Color;", "vec2 in_Color = gl_Color.xy;",
540            "in vec3 in_Color;", "vec3 in_Color = gl_Color.xyz;",
541            "in vec4 in_Color;", "vec4 in_Color = gl_Color.xyzw;",
542
543            "in vec2 in_MultiTexCoord0;",
544               "vec2 in_MultiTexCoord0 = gl_MultiTexCoord0.xy;",
545            "in vec2 in_MultiTexCoord1;",
546               "vec2 in_MultiTexCoord1 = gl_MultiTexCoord1.xy;",
547            "in vec2 in_MultiTexCoord2;",
548               "vec2 in_MultiTexCoord2 = gl_MultiTexCoord2.xy;",
549            "in vec2 in_MultiTexCoord3;",
550               "vec2 in_MultiTexCoord3 = gl_MultiTexCoord3.xy;",
551            "in vec2 in_MultiTexCoord4;",
552               "vec2 in_MultiTexCoord4 = gl_MultiTexCoord4.xy;",
553            "in vec2 in_MultiTexCoord5;",
554               "vec2 in_MultiTexCoord5 = gl_MultiTexCoord5.xy;",
555            "in vec2 in_MultiTexCoord6;",
556               "vec2 in_MultiTexCoord6 = gl_MultiTexCoord6.xy;",
557            "in vec2 in_MultiTexCoord7;",
558               "vec2 in_MultiTexCoord7 = gl_MultiTexCoord7.xy;",
559#endif
560
561            NULL
562        };
563
564        for (char const * const *rep = main_replaces; rep[0]; rep += 2)
565        {
566            char *match = strstr(dst, rep[0]);
567            if (match && match < main)
568            {
569                size_t l0 = strlen(rep[0]);
570                size_t l1 = strlen(rep[1]);
571                memmove(main + l1, main, end - main);
572                memcpy(main, rep[1], l1);
573                memset(match, ' ', l0);
574                main += l1;
575                end += l1;
576            }
577        }
578
579        /* Perform small replaces */
580        char const * const fast_replaces[] =
581        {
582            "#version 130", "#version 120",
583            "in vec2", vert ? "attribute vec2" : "varying vec2",
584            "in vec3", vert ? "attribute vec3" : "varying vec3",
585            "in vec4", vert ? "attribute vec4" : "varying vec4",
586            "in mat4", vert ? "attribute mat4" : "varying mat4",
587            "out vec2", "varying vec2",
588            "out vec3", "varying vec3",
589            "out vec4", "varying vec4",
590            "out mat4", "varying mat4",
591            NULL
592        };
593
594        for (char const * const *rep = fast_replaces; rep[0]; rep += 2)
595        {
596            char *match;
597            while ((match = strstr(dst, rep[0])))
598            {
599                size_t l0 = strlen(rep[0]);
600                size_t l1 = strlen(rep[1]);
601
602                if (l1 > l0)
603                    memmove(match + l1, match + l0, (end - match) - l0);
604                memcpy(match, rep[1], l1);
605                if (l1 < l0)
606                    memset(match + l0, ' ', l1 - l0);
607                end += l1 - l0;
608            }
609        }
610    }
611}
612
613} /* namespace lol */
614
Note: See TracBrowser for help on using the repository browser.