Changeset 1987


Ignore:
Timestamp:
Oct 8, 2012, 1:09:29 AM (5 years ago)
Author:
sam
Message:

gpu: put the texture creation code in its own class; for now a lot of things are
hardcoded and only the fractal demo uses it, but everything will eventually switch.

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r1938 r1987  
    5151    gpu/vertexbuffer.cpp gpu/vertexbuffer.h \
    5252    gpu/framebuffer.cpp gpu/framebuffer.h \
     53    gpu/texture.cpp gpu/texture.h \
    5354    \
    5455    gpu/defaultmaterial.lolfx \
  • trunk/src/core.h

    r1927 r1987  
    116116#include "gpu/lolfx.h"
    117117#include "gpu/shader.h"
     118#include "gpu/texture.h"
    118119#include "gpu/indexbuffer.h"
    119120#include "gpu/vertexbuffer.h"
  • trunk/src/lolcore.vcxproj

    r1927 r1987  
    255255    <ClCompile Include="gpu\lolfx.cpp" />
    256256    <ClCompile Include="gpu\shader.cpp" />
     257    <ClCompile Include="gpu\texture.cpp" />
    257258    <ClCompile Include="gpu\vertexbuffer.cpp" />
    258259    <ClCompile Include="gradient.cpp" />
     
    568569    <ClInclude Include="gpu\lolfx.h" />
    569570    <ClInclude Include="gpu\shader.h" />
     571    <ClInclude Include="gpu\texture.h" />
    570572    <ClInclude Include="gpu\vertexbuffer.h" />
    571573    <ClInclude Include="gradient.h" />
  • trunk/src/lolcore.vcxproj.filters

    r1927 r1987  
    9898      <Filter>src\gpu</Filter>
    9999    </ClCompile>
     100    <ClCompile Include="gpu\texture.cpp">
     101      <Filter>src\gpu</Filter>
     102    </ClCompile>
    100103    <ClCompile Include="gpu\vertexbuffer.cpp">
    101104      <Filter>src\gpu</Filter>
     
    668671    </ClInclude>
    669672    <ClInclude Include="gpu\shader.h">
     673      <Filter>src\gpu</Filter>
     674    </ClInclude>
     675    <ClInclude Include="gpu\texture.h">
    670676      <Filter>src\gpu</Filter>
    671677    </ClInclude>
  • trunk/tutorial/11_fractal.cpp

    r1963 r1987  
    1616
    1717#include "core.h"
    18 #include "lolgl.h"
    1918#include "loldebug.h"
    2019
    2120using namespace lol;
    2221
    23 #if defined _WIN32
    24 #   include <direct.h>
    25 #   if defined USE_D3D9
    26 #       define FAR
    27 #       define NEAR
    28 #       include <d3d9.h>
    29 #   endif
    30 #endif
    31 
    3222extern char const *lolfx_11_fractal;
    33 
    34 #if defined USE_D3D9
    35 extern IDirect3DDevice9 *g_d3ddevice;
    36 #elif defined _XBOX
    37 extern D3DDevice *g_d3ddevice;
    38 #elif __CELLOS_LV2__
    39 static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
    40 static GLenum const TEXTURE_FORMAT = GL_BGRA;
    41 static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
    42 #elif defined __native_client__ || defined HAVE_GLES_2X
    43 static GLint const INTERNAL_FORMAT = GL_RGBA;
    44 static GLenum const TEXTURE_FORMAT = GL_RGBA;
    45 static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE;
    46 #else
    47 /* Seems efficient for little endian textures */
    48 static GLint const INTERNAL_FORMAT = GL_RGBA;
    49 static GLenum const TEXTURE_FORMAT = GL_BGRA;
    50 static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
    51 #endif
    5223
    5324class Fractal : public WorldEntity
     
    468439        if (!m_ready)
    469440        {
    470 #if !defined _XBOX && !defined USE_D3D9
    471441            /* Create a texture of half the width and twice the height
    472442             * so that we can upload four different subimages each frame. */
    473             glGenTextures(1, &m_texid);
    474             glBindTexture(GL_TEXTURE_2D, m_texid);
    475             glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
    476                          m_size.x / 2, m_size.y * 2, 0,
    477                          TEXTURE_FORMAT, TEXTURE_TYPE, &m_pixels[0]);
    478 #   if defined __CELLOS_LV2__
    479             /* We need this hint because by default the storage type is
    480              * GL_TEXTURE_SWIZZLED_GPU_SCE. */
    481             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
    482                             GL_TEXTURE_TILED_GPU_SCE);
    483 #   endif
    484             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    485             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    486 #elif defined _XBOX
    487             /* By default the X360 will swizzle the texture. Ask for linear. */
    488             g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
    489                                        D3DUSAGE_WRITEONLY, D3DFMT_LIN_A8R8G8B8,
    490                                        D3DPOOL_DEFAULT, &m_tex, NULL);
    491 #else
    492             g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
    493                                        D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
    494                                        D3DPOOL_SYSTEMMEM, &m_tex, NULL);
    495 #endif
     443            m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2));
     444
     445            /* Ensure the texture data is complete at least once, otherwise
     446             * uploading subimages will not work. */
     447            m_texture->SetData(&m_pixels[0]);
    496448
    497449            m_shader = Shader::Create(lolfx_11_fractal);
     
    521473        }
    522474
    523 #if defined _XBOX || defined USE_D3D9
    524 
    525 #else
    526 #   if !defined HAVE_GLES_2X
    527         glEnable(GL_TEXTURE_2D);
    528 #   endif
    529         glBindTexture(GL_TEXTURE_2D, m_texid);
    530 #endif
     475        m_texture->Bind();
    531476
    532477        if (m_dirty[m_frame])
     
    537482            m_dirty[m_frame]--;
    538483
    539 #if defined _XBOX || defined USE_D3D9
    540             D3DLOCKED_RECT rect;
    541 #   if defined _XBOX
    542             m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE);
    543 #   else
    544             m_tex->LockRect(0, &rect, NULL,
    545                             D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
    546 #   endif
    547             for (int j = 0; j < m_size.y * 2; j++)
    548             {
    549                 u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4;
    550                 for (int i = 0; i < m_size.x / 2; i++)
    551                     line[i] = m_pixels[m_size.x / 2 * j + i];
    552             }
    553             m_tex->UnlockRect(0);
    554 #elif defined __CELLOS_LV2__
     484#if defined __CELLOS_LV2__
    555485            /* glTexSubImage2D is extremely slow on the PS3, to the point
    556486             * that uploading the whole texture is 40 times faster. */
    557             glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
    558                          m_size.x / 2, m_size.y * 2, 0,
    559                          TEXTURE_FORMAT, TEXTURE_TYPE, &m_pixels[0]);
     487            m_texture->SetData(&m_pixels[0]);
    560488#else
    561             glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
    562                             m_size.x / 2, m_size.y / 2,
    563                             TEXTURE_FORMAT, TEXTURE_TYPE,
    564                             &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
     489            m_texture->SetSubData(ivec2(0, m_frame * m_size.y / 2),
     490                                  m_size / 2,
     491                                  &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
    565492#endif
    566493        }
     
    573500        m_vdecl->SetStream(m_vbo, m_vertexattrib);
    574501        m_vdecl->SetStream(m_tbo, m_texattrib);
    575 #if defined _XBOX || defined USE_D3D9
    576         g_d3ddevice->SetTexture(0, m_tex);
    577         g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
    578 #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
    579 #else
    580 #endif
     502        m_texture->Bind();
    581503        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
    582504        m_vdecl->Unbind();
     
    600522    VertexDeclaration *m_vdecl;
    601523    VertexBuffer *m_vbo, *m_tbo;
    602 #if defined USE_D3D9
    603     IDirect3DTexture9 *m_tex;
    604 #elif defined _XBOX
    605     D3DTexture *m_tex;
    606 #else
    607     GLuint m_texid;
    608 #endif
     524    Texture *m_texture;
     525
    609526    int m_frame, m_slices, m_dirty[4];
    610527    bool m_ready, m_drag;
Note: See TracChangeset for help on using the changeset viewer.