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

Last change on this file since 1241 was 1241, checked in by sam, 9 years ago

gpu: fix a great lot of Direct3D problems, spotted using PIX.

  • Property svn:keywords set to Id
File size: 18.3 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
431void Shader::Unbind() const
432{
433#if defined USE_D3D9 || defined _XBOX
434    HRESULT hr;
435    hr = g_d3ddevice->SetVertexShader(NULL);
436    hr = g_d3ddevice->SetPixelShader(NULL);
437#elif !defined __CELLOS_LV2__
438    /* FIXME: untested */
439    glUseProgram(0);
440#else
441    /* FIXME: untested */
442    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX));
443    cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT));
444#endif
445}
446
447Shader::~Shader()
448{
449#if defined USE_D3D9 || defined _XBOX
450    data->vert_shader->Release();
451    data->vert_table->Release();
452    data->frag_shader->Release();
453    data->frag_table->Release();
454#elif !defined __CELLOS_LV2__
455    glDetachShader(data->prog_id, data->vert_id);
456    glDetachShader(data->prog_id, data->frag_id);
457    glDeleteShader(data->vert_id);
458    glDeleteShader(data->frag_id);
459    glDeleteProgram(data->prog_id);
460#else
461    cgDestroyProgram(data->vert_id);
462    cgDestroyProgram(data->frag_id);
463#endif
464    delete data;
465}
466
467/* Try to detect shader compiler features */
468int ShaderData::GetVersion()
469{
470    static int version = 0;
471
472#if !defined USE_D3D9 && !defined _XBOX && !defined __CELLOS_LV2__
473    if (!version)
474    {
475        char buf[4096];
476        GLsizei len;
477
478        int id = glCreateShader(GL_VERTEX_SHADER);
479
480        /* Can we compile 1.30 shaders? */
481        char const *test130 =
482            "#version 130\n"
483            "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
484        glShaderSource(id, 1, &test130, NULL);
485        glCompileShader(id);
486        glGetShaderInfoLog(id, sizeof(buf), &len, buf);
487        if (len <= 0)
488            version = 130;
489
490        /* If not, can we compile 1.20 shaders? */
491        if (!version)
492        {
493            char const *test120 =
494                "#version 120\n"
495                "void main() { gl_Position = vec4(0.0, 0.0, 0.0, 0.0); }";
496            glShaderSource(id, 1, &test120, NULL);
497            glCompileShader(id);
498            glGetShaderInfoLog(id, sizeof(buf), &len, buf);
499            if (len <= 0)
500                version = 120;
501        }
502
503        /* Otherwise, assume we can compile 1.10 shaders. */
504        if (!version)
505            version = 110;
506
507        glDeleteShader(id);
508    }
509#endif
510
511    return version;
512}
513
514/* Simple shader source patching for old GLSL versions.
515 * If supported version is 1.30, do nothing.
516 * If supported version is 1.20:
517 *  - replace "#version 130" with "#version 120"
518 */
519void ShaderData::Patch(char *dst, char const *vert, char const *frag)
520{
521    int ver_driver = GetVersion();
522
523    strcpy(dst, vert ? vert : frag);
524    if (ver_driver >= 130)
525        return;
526
527    int ver_shader = 110;
528    char *parser = strstr(dst, "#version");
529    if (parser)
530        ver_shader = atoi(parser + strlen("#version"));
531
532    if (ver_shader > 120 && ver_driver <= 120)
533    {
534        char const *end = dst + strlen(dst) + 1;
535
536        /* Find main() */
537        parser = strstr(dst, "main");
538        if (!parser) return;
539        parser = strstr(parser, "(");
540        if (!parser) return;
541        parser = strstr(parser, ")");
542        if (!parser) return;
543        parser = strstr(parser, "{");
544        if (!parser) return;
545        char *main = parser + 1;
546
547        /* Perform main() replaces */
548        char const * const main_replaces[] =
549        {
550#if 0
551            "in vec2 in_Vertex;", "vec2 in_Vertex = gl_Vertex.xy;",
552            "in vec3 in_Vertex;", "vec3 in_Vertex = gl_Vertex.xyz;",
553            "in vec4 in_Vertex;", "vec4 in_Vertex = gl_Vertex.xyzw;",
554
555            "in vec2 in_Color;", "vec2 in_Color = gl_Color.xy;",
556            "in vec3 in_Color;", "vec3 in_Color = gl_Color.xyz;",
557            "in vec4 in_Color;", "vec4 in_Color = gl_Color.xyzw;",
558
559            "in vec2 in_MultiTexCoord0;",
560               "vec2 in_MultiTexCoord0 = gl_MultiTexCoord0.xy;",
561            "in vec2 in_MultiTexCoord1;",
562               "vec2 in_MultiTexCoord1 = gl_MultiTexCoord1.xy;",
563            "in vec2 in_MultiTexCoord2;",
564               "vec2 in_MultiTexCoord2 = gl_MultiTexCoord2.xy;",
565            "in vec2 in_MultiTexCoord3;",
566               "vec2 in_MultiTexCoord3 = gl_MultiTexCoord3.xy;",
567            "in vec2 in_MultiTexCoord4;",
568               "vec2 in_MultiTexCoord4 = gl_MultiTexCoord4.xy;",
569            "in vec2 in_MultiTexCoord5;",
570               "vec2 in_MultiTexCoord5 = gl_MultiTexCoord5.xy;",
571            "in vec2 in_MultiTexCoord6;",
572               "vec2 in_MultiTexCoord6 = gl_MultiTexCoord6.xy;",
573            "in vec2 in_MultiTexCoord7;",
574               "vec2 in_MultiTexCoord7 = gl_MultiTexCoord7.xy;",
575#endif
576
577            NULL
578        };
579
580        for (char const * const *rep = main_replaces; rep[0]; rep += 2)
581        {
582            char *match = strstr(dst, rep[0]);
583            if (match && match < main)
584            {
585                size_t l0 = strlen(rep[0]);
586                size_t l1 = strlen(rep[1]);
587                memmove(main + l1, main, end - main);
588                memcpy(main, rep[1], l1);
589                memset(match, ' ', l0);
590                main += l1;
591                end += l1;
592            }
593        }
594
595        /* Perform small replaces */
596        char const * const fast_replaces[] =
597        {
598            "#version 130", "#version 120",
599            "in vec2", vert ? "attribute vec2" : "varying vec2",
600            "in vec3", vert ? "attribute vec3" : "varying vec3",
601            "in vec4", vert ? "attribute vec4" : "varying vec4",
602            "in mat4", vert ? "attribute mat4" : "varying mat4",
603            "out vec2", "varying vec2",
604            "out vec3", "varying vec3",
605            "out vec4", "varying vec4",
606            "out mat4", "varying mat4",
607            NULL
608        };
609
610        for (char const * const *rep = fast_replaces; rep[0]; rep += 2)
611        {
612            char *match;
613            while ((match = strstr(dst, rep[0])))
614            {
615                size_t l0 = strlen(rep[0]);
616                size_t l1 = strlen(rep[1]);
617
618                if (l1 > l0)
619                    memmove(match + l1, match + l0, (end - match) - l0);
620                memcpy(match, rep[1], l1);
621                if (l1 < l0)
622                    memset(match + l0, ' ', l1 - l0);
623                end += l1 - l0;
624            }
625        }
626    }
627}
628
629} /* namespace lol */
630
Note: See TracBrowser for help on using the repository browser.