Changeset 1221


Ignore:
Timestamp:
Apr 12, 2012, 7:38:54 PM (9 years ago)
Author:
sam
Message:

gpu: port the texture and scene handling code to Direct3D. Not functional.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gpu/shader.cpp

    r1215 r1221  
    322322{
    323323#if defined USE_D3D9 || defined _XBOX
    324     SetUniform(uni, v);
     324    if (uni.flags & 1)
     325        g_d3ddevice->SetPixelShaderConstantI((UINT)uni.frag, &v[0], 1);
     326    if (uni.flags & 2)
     327        g_d3ddevice->SetVertexShaderConstantI((UINT)uni.vert, &v[0], 1);
    325328#elif !defined __CELLOS_LV2__
    326329    glUniform4i(uni.frag, v.x, v.y, v.z, v.w);
     
    374377void Shader::SetUniform(ShaderUniform const &uni, vec4 const &v)
    375378{
    376     /* FIXME: use the array versions of these functions */
    377379#if defined USE_D3D9 || defined _XBOX
    378380    if (uni.flags & 1)
     
    383385    glUniform4f(uni.frag, v.x, v.y, v.z, v.w);
    384386#else
     387    /* FIXME: use the array versions of these functions */
    385388    if (uni.frag)
    386389        cgGLSetParameter4f((CGparameter)uni.frag, v.x, v.y, v.z, v.w);
  • trunk/src/scene.cpp

    r1215 r1221  
    1919#   define WIN32_LEAN_AND_MEAN
    2020#   include <windows.h>
     21#   if defined USE_D3D9
     22#       define FAR
     23#       define NEAR
     24#       include <d3d9.h>
     25#   endif
    2126#endif
    2227
    2328#include "core.h"
    2429#include "lolgl.h"
     30
     31#if defined USE_D3D9
     32extern IDirect3DDevice9 *g_d3ddevice;
     33#elif defined _XBOX
     34extern D3DDevice *g_d3ddevice;
     35#endif
    2536
    2637namespace lol
     
    6172    float angle;
    6273
    63 #if defined USE_D3D9 || defined _XBOX
    64 #   if defined __GNUC__
    65 #       warning Scene not implemented
    66 #   else
    67 #      define STR0(x) #x
    68 #      define STR(x) STR0(x)
    69 #      pragma message(__FILE__ "(" STR(__LINE__) "): warning: Scene not implemented")
    70 #      undef STR
    71 #      undef STR0
    72 #   endif
    73 #else
    74 #if defined HAVE_GL_2X && !defined __APPLE__
     74#if defined USE_D3D9
     75    IDirect3DVertexDeclaration9 *m_vdecl;
     76    IDirect3DVertexBuffer9 **bufs;
     77#elif defined _XBOX
     78    D3DVertexDeclaration *m_vdecl;
     79    D3DVertexBuffer **bufs;
     80#else
     81#   if defined HAVE_GL_2X && !defined __APPLE__
    7582    GLuint vao;
    76 #endif
     83#   endif
    7784    GLuint *bufs;
     85#endif
    7886    int nbufs;
    79 #endif
    8087
    8188    static Scene *scene;
     
    95102    data->angle = angle;
    96103
    97 #if defined USE_D3D9 || defined _XBOX
    98     /* TODO */
    99 #else
    100104    data->bufs = 0;
    101105    data->nbufs = 0;
    102106
     107#if defined _XBOX || defined USE_D3D9
     108    D3DVERTEXELEMENT9 const elements[] =
     109    {
     110        { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
     111        { 1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
     112        D3DDECL_END()
     113    };
     114    g_d3ddevice->CreateVertexDeclaration(elements, &data->m_vdecl);
     115#else
    103116#   if defined HAVE_GL_2X && !defined __APPLE__
    104117    glGenVertexArrays(1, &data->vao);
     
    110123{
    111124#if defined USE_D3D9 || defined _XBOX
    112     /* TODO */
     125    /* FIXME: TODO */
    113126#else
    114127    /* FIXME: this must be done while the GL context is still active.
     
    141154void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale)
    142155{
    143 #if !defined USE_D3D9 && !defined _XBOX
    144156    if ((data->ntiles % 1024) == 0)
    145157        data->tiles = (Tile *)realloc(data->tiles,
     
    153165    data->tiles[data->ntiles].scale = scale;
    154166    data->ntiles++;
    155 #endif
    156167}
    157168
    158169void Scene::Render() // XXX: rename to Blit()
    159170{
    160 #if !defined USE_D3D9 && !defined _XBOX /* No WPOS on Xbox, what about Win32? */
    161171    if (!stdshader)
    162172    {
    163 #if !defined _XBOX && !defined __CELLOS_LV2__
     173#if !defined _XBOX && !defined __CELLOS_LV2__ && !defined USE_D3D9
    164174        stdshader = Shader::Create(
    165175#if !defined HAVE_GLES_2X
     
    258268
    259269            "void main(float2 in_TexCoord : TEXCOORD0,"
     270#if 0
    260271            "          float4 in_FragCoord : WPOS,"
     272#endif
    261273            "          uniform sampler2D tex,"
    262274            "          out float4 out_FragColor : COLOR)"
     
    298310            "    out_FragColor = col;"
    299311            "}");
    300 #endif
    301312    }
    302313
     
    326337
    327338    ShaderUniform uni_mat, uni_tex;
     339#if defined USE_D3D9 || defined _XBOX
     340    /* Nothing? */
     341#else
    328342    int attr_pos, attr_tex;
    329343#if !defined __CELLOS_LV2__
    330344    attr_pos = stdshader->GetAttribLocation("in_Position");
    331345    attr_tex = stdshader->GetAttribLocation("in_TexCoord");
     346#endif
    332347#endif
    333348
     
    341356    stdshader->SetUniform(uni_mat, data->model_matrix);
    342357
     358#if defined USE_D3D9 || defined _XBOX
     359    //g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
     360    g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
     361    g_d3ddevice->SetVertexDeclaration(data->m_vdecl);
     362#else
    343363    uni_tex = stdshader->GetUniformLocation("in_Texture");
    344364    stdshader->SetUniform(uni_tex, 0);
    345365
    346 #if defined USE_D3D9 || defined _XBOX
    347     /* TODO */
    348 #else
    349366#if !defined HAVE_GLES_2X
    350367    glEnable(GL_TEXTURE_2D);
     
    362379    for (int buf = 0, i = 0, n; i < data->ntiles; i = n, buf += 2)
    363380    {
    364 #if defined USE_D3D9 || defined _XBOX
    365     /* TODO */
    366 #else
    367381        /* Generate new vertex / texture coord buffers if necessary */
    368382        if (buf + 2 > data->nbufs)
    369383        {
     384#if defined USE_D3D9
     385            data->bufs = (IDirect3DVertexBuffer9 **)realloc(data->bufs, (buf + 2) * sizeof(IDirect3DVertexBuffer9 *));
     386#elif defined _XBOX
     387            data->bufs = (D3DVertexBuffer **)realloc(data->bufs, (buf + 2) * sizeof(D3DVertexBuffer *));
     388#else
    370389            data->bufs = (uint32_t *)realloc(data->bufs, (buf + 2) * sizeof(uint32_t));
    371390            glGenBuffers(buf + 2 - data->nbufs, data->bufs + data->nbufs);
     391#endif
    372392            data->nbufs = buf + 2;
     393        }
     394#if defined USE_D3D9 || defined _XBOX
     395        else
     396        {
     397            data->bufs[buf]->Release();
     398            data->bufs[buf + 1]->Release();
    373399        }
    374400#endif
     
    380406
    381407        /* Create a vertex array object */
    382         float *vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
    383         float *texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
     408        float *vertex, *texture;
     409#if defined USE_D3D9 || defined _XBOX
     410        if (FAILED(g_d3ddevice->CreateVertexBuffer(6 * 3 * (n - i) * sizeof(float), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &data->bufs[buf], NULL)))
     411            exit(0);
     412        if (FAILED(data->bufs[buf]->Lock(0, 0, (void **)&vertex, 0)))
     413            exit(0);
     414        if (FAILED(g_d3ddevice->CreateVertexBuffer(6 * 2 * (n - i) * sizeof(float), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &data->bufs[buf + 1], NULL)))
     415            exit(0);
     416        if (FAILED(data->bufs[buf + 1]->Lock(0, 0, (void **)&texture, 0)))
     417            exit(0);
     418#else
     419        vertex = (float *)malloc(6 * 3 * (n - i) * sizeof(float));
     420        texture = (float *)malloc(6 * 2 * (n - i) * sizeof(float));
     421#endif
    384422
    385423        for (int j = i; j < n; j++)
     
    391429        }
    392430
     431#if defined USE_D3D9 || defined _XBOX
     432        data->bufs[buf]->Unlock();
     433        data->bufs[buf + 1]->Unlock();
     434#endif
     435
    393436        stdshader->Bind();
    394437
     
    397440
    398441        /* Bind vertex, color and texture coordinate buffers */
    399 #if defined HAVE_GL_2X && !defined __APPLE__
     442#if defined USE_D3D9 || defined _XBOX
     443        g_d3ddevice->SetStreamSource(0, data->bufs[data->nbufs - 2], 0, 6 * 3 * (n - i) * sizeof(float));
     444        g_d3ddevice->SetStreamSource(1, data->bufs[data->nbufs - 1], 0, 6 * 2 * (n - i) * sizeof(float));
     445        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, (n - i) * 6);
     446#else
     447#   if defined HAVE_GL_2X && !defined __APPLE__
    400448        glBindVertexArray(data->vao);
    401 #endif
    402 #if defined USE_D3D9 || defined _XBOX
    403     /* TODO */
    404 #else
    405 #if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
     449#   endif
     450#   if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
    406451        glEnableVertexAttribArray(attr_pos);
    407452        glEnableVertexAttribArray(attr_tex);
     
    416461                     texture, GL_STATIC_DRAW);
    417462        glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
    418 #else
     463#   else
    419464        glEnableClientState(GL_VERTEX_ARRAY);
    420465        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     
    422467        glVertexPointer(3, GL_FLOAT, 0, vertex);
    423468        glTexCoordPointer(2, GL_FLOAT, 0, texture);
    424 #endif
     469#   endif
    425470
    426471        /* Draw arrays */
    427472        glDrawArrays(GL_TRIANGLES, 0, (n - i) * 6);
    428473
    429 #if defined HAVE_GL_2X && !defined __APPLE__
     474#   if defined HAVE_GL_2X && !defined __APPLE__
    430475        glBindVertexArray(0);
    431 #endif
    432 #if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
     476#   endif
     477#   if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
    433478        glDisableVertexAttribArray(attr_pos);
    434479        glDisableVertexAttribArray(attr_tex);
    435 #else
     480#   else
    436481        glDisableClientState(GL_VERTEX_ARRAY);
    437482        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    438 #endif
    439 #endif
     483#   endif
    440484
    441485        free(vertex);
    442486        free(texture);
     487#endif
    443488    }
    444489
  • trunk/src/tileset.cpp

    r1208 r1221  
    2121#   define WIN32_LEAN_AND_MEAN
    2222#   include <windows.h>
     23#   if defined USE_D3D9
     24#       define FAR
     25#       define NEAR
     26#       include <d3d9.h>
     27#   endif
    2328#endif
    2429
     
    2732
    2833using namespace std;
     34
     35#if defined USE_D3D9
     36extern IDirect3DDevice9 *g_d3ddevice;
     37#elif defined _XBOX
     38extern D3DDevice *g_d3ddevice;
     39#endif
    2940
    3041namespace lol
     
    4758
    4859    Image *img;
    49 #if defined USE_D3D9 || defined _XBOX
    50 #   define STR0(x) #x
    51 #   define STR(x) STR0(x)
    52 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet not implemented")
    53 #   undef STR
    54 #   undef STR0
    55 #else
    56     GLuint texture;
     60#if defined USE_D3D9
     61    IDirect3DTexture9 *m_tex;
     62#elif defined _XBOX
     63    D3DTexture *m_tex;
     64#else
     65    GLuint m_tex;
    5766#endif
    5867};
     
    7079
    7180    data->tiles = NULL;
    72 #if defined USE_D3D9 || defined _XBOX
    73 #   define STR0(x) #x
    74 #   define STR(x) STR0(x)
    75 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TileSet() not implemented")
    76 #   undef STR
    77 #   undef STR0
    78 #else
    79     data->texture = 0;
    80 #endif
     81    data->m_tex = 0;
    8182    data->img = new Image(path);
    8283    data->isize = data->img->GetSize();
     
    119120        if (data->img)
    120121            delete data->img;
    121 #if defined USE_D3D9 || defined _XBOX
    122 #   define STR0(x) #x
    123 #   define STR(x) STR0(x)
    124 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TickDraw() not implemented")
    125 #   undef STR
    126 #   undef STR0
    127 #else
    128122        else
    129             glDeleteTextures(1, &data->texture);
     123#if defined USE_D3D9 || defined _XBOX
     124            /* FIXME: is it really the correct call? */
     125            data->m_tex->Release();
     126#else
     127            glDeleteTextures(1, &data->m_tex);
    130128#endif
    131129    }
     
    133131    {
    134132#if defined USE_D3D9 || defined _XBOX
    135 #   define STR0(x) #x
    136 #   define STR(x) STR0(x)
    137 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TickDraw() not implemented")
    138 #   undef STR
    139 #   undef STR0
     133        D3DFORMAT format;
    140134#else
    141135        GLuint format;
     136#endif
    142137        int planes;
    143138
     
    145140        {
    146141        case Image::FORMAT_RGB:
     142#if defined USE_D3D9
     143           format = D3DFMT_R8G8B8;
     144#elif defined _XBOX
     145           format = D3DFMT_LIN_R8G8B8; /* FIXME */
     146#else
    147147           format = GL_RGB;
     148#endif
    148149           planes = 3;
    149150           break;
    150151        case Image::FORMAT_RGBA:
    151152        default:
     153#if defined USE_D3D9
     154           format = D3DFMT_A8R8G8B8;
     155#elif defined _XBOX
     156           format = D3DFMT_LIN_A8R8G8B8;
     157#else
    152158           format = GL_RGBA;
     159#endif
    153160           planes = 4;
    154161           break;
     
    169176        }
    170177
    171         glGenTextures(1, &data->texture);
     178#if defined USE_D3D9 || defined _XBOX
     179        D3DLOCKED_RECT rect;
     180#   if defined USE_D3D9
     181        g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_DYNAMIC, format,
     182                                   D3DPOOL_SYSTEMMEM, &data->m_tex, NULL);
     183        data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
     184#   elif defined _XBOX
     185        /* By default the X360 will swizzle the texture. Ask for linear. */
     186        g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_WRITEONLY, format,
     187                                   D3DPOOL_DEFAULT, &data->m_tex, NULL);
     188        data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE);
     189#   endif
     190        for (int j = 0; j < h; j++)
     191            memcpy((uint8_t *)rect.pBits + j * rect.Pitch, pixels + w * j * 4, w * 4);
     192        data->m_tex->UnlockRect(0);
     193#else
     194        glGenTextures(1, &data->m_tex);
    172195        glEnable(GL_TEXTURE_2D);
    173         glBindTexture(GL_TEXTURE_2D, data->texture);
     196        glBindTexture(GL_TEXTURE_2D, data->m_tex);
    174197
    175198        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
     
    178201        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    179202        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     203#endif
    180204
    181205        if (pixels != data->img->GetData())
    182206            free(pixels);
    183 #endif
    184207        delete data->img;
    185208        data->img = NULL;
     
    204227void TileSet::Bind()
    205228{
    206 #if defined USE_D3D9 || defined _XBOX
    207 #   define STR0(x) #x
    208 #   define STR(x) STR0(x)
    209 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::Bind() not implemented")
    210 #   undef STR
    211 #   undef STR0
    212 #else
    213     if (!data->img && data->texture)
    214     {
     229    if (!data->img && data->m_tex)
     230    {
     231#if defined USE_D3D9 || defined _XBOX
     232        g_d3ddevice->SetTexture(0, data->m_tex);
     233#else
    215234        glActiveTexture(GL_TEXTURE0);
    216         glBindTexture(GL_TEXTURE_2D, data->texture);
    217     }
    218 #endif
     235        glBindTexture(GL_TEXTURE_2D, data->m_tex);
     236#endif
     237    }
    219238}
    220239
     
    229248    int dz = o ? data->size.y * scale.y : 0;
    230249
    231 #if defined USE_D3D9 || defined _XBOX
    232 #   define STR0(x) #x
    233 #   define STR(x) STR0(x)
    234 #   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TileSet() not implemented")
    235 #   undef STR
    236 #   undef STR0
    237 #else
    238     if (!data->img && data->texture)
     250    if (!data->img && data->m_tex)
    239251    {
    240252        float tmp[10];
     
    281293        memset(texture, 0, 2 * sizeof(float));
    282294    }
    283 #endif
    284295}
    285296
Note: See TracChangeset for help on using the changeset viewer.