Changeset 1213


Ignore:
Timestamp:
Apr 10, 2012, 8:39:16 PM (11 years ago)
Author:
sam
Message:

win32: add Direct3D texture creation code in the fractal zoomer. Shader
support is still missing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/tutorial/tut03.cpp

    r1178 r1213  
    2323using namespace lol;
    2424
     25#if defined _WIN32 && defined USE_D3D9
     26#   define FAR
     27#   define NEAR
     28#   include <d3d9.h>
     29#endif
     30
    2531#if USE_SDL && defined __APPLE__
    2632#   include <SDL_main.h>
     
    3238#endif
    3339
    34 #ifdef __CELLOS_LV2__
     40#if defined USE_D3D9
     41extern IDirect3DDevice9 *g_d3ddevice;
     42#elif defined _XBOX
     43extern D3DDevice *g_d3ddevice;
     44#elif __CELLOS_LV2__
    3545static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
    3646static GLenum const TEXTURE_FORMAT = GL_BGRA;
     
    444454        if (!m_ready)
    445455        {
     456#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
    446457            /* Create a texture of half the width and twice the height
    447458             * so that we can upload four different subimages each frame. */
     
    451462                         m_size.x / 2, m_size.y * 2, 0,
    452463                         TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
    453 #if defined __CELLOS_LV2__
     464#   if defined __CELLOS_LV2__
    454465            /* We need this hint because by default the storage type is
    455466             * GL_TEXTURE_SWIZZLED_GPU_SCE. */
    456467            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
    457468                            GL_TEXTURE_TILED_GPU_SCE);
    458 #endif
     469#   endif
    459470            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    460471            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     472#else
     473            g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
     474                                       D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
     475                                       D3DPOOL_SYSTEMMEM, &m_tex, NULL);
     476#endif
    461477
    462478            m_shader = Shader::Create(
    463 #if !defined __CELLOS_LV2__
    464 #if !defined HAVE_GLES_2X
     479#if !defined __CELLOS_LV2__ && !defined _XBOX && !defined USE_D3D9
     480#   if !defined HAVE_GLES_2X
    465481                "#version 120\n"
    466 #else
     482#   else
    467483                "precision highp float;"
    468 #endif
     484#   endif
    469485                ""
    470486                "uniform mat4 u_ZoomSettings;"
     
    511527                "}",
    512528
    513 #if !defined HAVE_GLES_2X
     529#   if !defined HAVE_GLES_2X
    514530                "#version 120\n"
    515 #else
     531#   else
    516532                "precision highp float;"
    517 #endif
     533#   endif
    518534                ""
    519535                "uniform vec4 u_TexelSize;"
     
    540556                "    ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);"
    541557                ""
    542 #if 1
     558#   if 1
    543559                "\n#if 0\n" /* XXX: disabled until we can autodetect i915 */
    544560                     /* t1.x <-- dd.x > dd.y */
     
    577593                     /* Nearest neighbour */
    578594                "    gl_FragColor = texture2D(u_Texture, ret.xy);"
    579 #else
     595#   else
    580596                     /* Alternate version: some kind of linear interpolation */
    581597                "    vec4 p0 = texture2D(u_Texture, vec2(rx.x, ry.x));"
     
    585601                "    gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)"
    586602                "          * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);"
    587 #endif
     603#   endif
    588604                "}"
    589605#else
     
    648664#endif
    649665            );
     666#if !defined _XBOX && !defined USE_D3D9
    650667            m_vertexattrib = m_shader->GetAttribLocation("a_Vertex");
    651668            m_texattrib = m_shader->GetAttribLocation("a_TexCoord");
     669#endif
    652670            m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
    653671#if defined __CELLOS_LV2__
     
    658676            m_ready = true;
    659677
    660 #if !defined __CELLOS_LV2__ && !defined __ANDROID__
     678#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined _XBOX && !defined USE_D3D9
    661679            /* Method 1: store vertex buffer on the GPU memory */
    662680            glGenBuffers(1, &m_vbo);
     
    668686            glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
    669687                         GL_STATIC_DRAW);
    670 #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
     688#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined _XBOX && !defined USE_D3D9
    671689            /* Method 2: upload vertex information at each frame */
     690#elif defined _XBOX || defined USE_D3D9
     691            D3DVERTEXELEMENT9 const elements[] =
     692            {
     693                { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
     694                { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
     695                D3DDECL_END()
     696            };
     697            g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl);
     698
     699            if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL)))
     700                exit(0);
     701
     702            vec2 *tmp1;
     703            if (FAILED(m_vbo->Lock(0, 0, (void **)&tmp1, 0)))
     704                exit(0);
     705            memcpy(tmp1, vertices, sizeof(vertices));
     706            m_vbo->Unlock();
     707
     708            if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(texcoords), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_tbo, NULL)))
     709                exit(0);
     710
     711            vec2 *tmp2;
     712            if (FAILED(m_tbo->Lock(0, 0, (void **)&tmp2, 0)))
     713                exit(0);
     714            memcpy(tmp2, texcoords, sizeof(texcoords));
     715            m_tbo->Unlock();
    672716#else
    673717#endif
     
    676720        }
    677721
    678 #if !defined HAVE_GLES_2X
     722#if defined _XBOX || defined USE_D3D9
     723
     724#else
     725#   if !defined HAVE_GLES_2X
    679726        glEnable(GL_TEXTURE_2D);
    680 #endif
     727#   endif
    681728        glBindTexture(GL_TEXTURE_2D, m_texid);
     729#endif
    682730
    683731        if (m_dirty[m_frame])
     
    688736            m_dirty[m_frame]--;
    689737
    690 #ifdef __CELLOS_LV2__
     738#if defined _XBOX || defined USE_D3D9
     739            D3DLOCKED_RECT rect;
     740            m_tex->LockRect(0, &rect, NULL,
     741                            D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
     742            for (int j = 0; j < m_size.y * 2; j++)
     743            {
     744                u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4;
     745                for (int i = 0; i < m_size.x / 2; j++)
     746                    line[i] = m_pixels[m_size.x / 2 * j + i];
     747            }
     748            m_tex->UnlockRect(0);
     749#elif defined __CELLOS_LV2__
    691750            /* glTexSubImage2D is extremely slow on the PS3, to the point
    692751             * that uploading the whole texture is 40 times faster. */
     
    709768        m_shader->SetUniform(m_screenuni, m_screen_settings);
    710769        m_shader->SetUniform(m_zoomuni, m_zoom_settings);
    711 #if !defined __CELLOS_LV2__ && !defined __ANDROID__
     770#if defined _XBOX || defined USE_D3D9
     771        g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
     772        g_d3ddevice->SetVertexDeclaration(m_vdecl);
     773        g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*vertices));
     774        g_d3ddevice->SetStreamSource(1, m_tbo, 0, sizeof(*texcoords));
     775#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
    712776        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    713777        glEnableVertexAttribArray(m_vertexattrib);
     
    728792#endif
    729793
     794#if defined _XBOX || defined USE_D3D9
     795        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6);
     796#else
    730797        glDrawArrays(GL_TRIANGLES, 0, 6);
    731 
    732 #if !defined __CELLOS_LV2__ && !defined __ANDROID__
     798#endif
     799
     800#if defined _XBOX || defined USE_D3D9
     801
     802#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
    733803        glDisableVertexAttribArray(m_vertexattrib);
    734804        glDisableVertexAttribArray(m_texattrib);
     
    755825    u8vec4 *m_pixels, *m_tmppixels, *m_palette;
    756826    Shader *m_shader;
     827#if defined USE_D3D9
     828    IDirect3DTexture9 *m_tex;
     829    IDirect3DVertexDeclaration9 *m_vdecl;
     830    IDirect3DVertexBuffer9 *m_vbo, *m_tbo;
     831#elif defined _XBOX
     832    D3DTexture *m_tex;
     833    D3DVertexDeclaration *m_vdecl;
     834    D3DVertexBuffer *m_vbo, *m_tbo;
     835#else
    757836    GLuint m_texid;
    758 #if !defined __CELLOS_LV2__ && !defined __ANDROID__
     837#   if !defined __CELLOS_LV2__ && !defined __ANDROID__
    759838    GLuint m_vbo, m_tbo;
    760839    GLuint m_tco;
     840#   endif
    761841#endif
    762842    int m_vertexattrib, m_texattrib, m_texeluni, m_screenuni, m_zoomuni;
Note: See TracChangeset for help on using the changeset viewer.