Rice GLES2 (from mupen64plus-ae) plugin. Compile but doesn't works well on the OpenPa...
[mupen64plus-pandora.git] / source / gles2rice / src / OGLTexture.cpp
1 /*
2 Copyright (C) 2003 Rice1964
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18
19 #include <stdlib.h>
20
21 #include "Config.h"
22 #include "Debugger.h"
23 #include "OGLDebug.h"
24 #include "OGLGraphicsContext.h"
25 #include "OGLTexture.h"
26 #include "TextureManager.h"
27
28 COGLTexture::COGLTexture(uint32 dwWidth, uint32 dwHeight, TextureUsage usage) :
29     CTexture(dwWidth,dwHeight,usage),
30     m_glFmt(GL_RGBA)
31 {
32     // FIXME: If usage is AS_RENDER_TARGET, we need to create pbuffer instead of regular texture
33
34     m_dwTextureFmt = TEXTURE_FMT_A8R8G8B8;  // Always use 32bit to load texture
35     glGenTextures( 1, &m_dwTextureName );
36     OPENGL_CHECK_ERRORS;
37
38     // Make the width and height be the power of 2
39     uint32 w;
40     for (w = 1; w < dwWidth; w <<= 1);
41     m_dwCreatedTextureWidth = w;
42     for (w = 1; w < dwHeight; w <<= 1);
43     m_dwCreatedTextureHeight = w;
44     
45     if (dwWidth*dwHeight > 256*256)
46         TRACE4("Large texture: (%d x %d), created as (%d x %d)", 
47             dwWidth, dwHeight,m_dwCreatedTextureWidth,m_dwCreatedTextureHeight);
48     
49     m_fYScale = (float)m_dwCreatedTextureHeight/(float)m_dwHeight;
50     m_fXScale = (float)m_dwCreatedTextureWidth/(float)m_dwWidth;
51
52     m_pTexture = malloc(m_dwCreatedTextureWidth * m_dwCreatedTextureHeight * GetPixelSize());
53
54     switch( options.textureQuality )
55     {
56     case TXT_QUALITY_DEFAULT:
57         if( options.colorQuality == TEXTURE_FMT_A4R4G4B4 ) 
58             m_glFmt = GL_RGBA4;
59         break;
60     case TXT_QUALITY_32BIT:
61         break;
62     case TXT_QUALITY_16BIT:
63             m_glFmt = GL_RGBA4;
64         break;
65     };
66     LOG_TEXTURE(TRACE2("New texture: (%d, %d)", dwWidth, dwHeight));
67 }
68
69 COGLTexture::~COGLTexture()
70 {
71     // FIXME: If usage is AS_RENDER_TARGET, we need to destroy the pbuffer
72
73     glDeleteTextures(1, &m_dwTextureName );
74     OPENGL_CHECK_ERRORS;
75     free(m_pTexture);
76     m_pTexture = NULL;
77     m_dwWidth = 0;
78     m_dwHeight = 0;
79 }
80
81 bool COGLTexture::StartUpdate(DrawInfo *di)
82 {
83     if (m_pTexture == NULL)
84         return false;
85
86     di->dwHeight = (uint16)m_dwHeight;
87     di->dwWidth = (uint16)m_dwWidth;
88     di->dwCreatedHeight = m_dwCreatedTextureHeight;
89     di->dwCreatedWidth = m_dwCreatedTextureWidth;
90     di->lpSurface = m_pTexture;
91     di->lPitch = GetPixelSize()*m_dwCreatedTextureWidth;
92
93     return true;
94 }
95
96 void COGLTexture::EndUpdate(DrawInfo *di)
97 {
98     COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext); // we need this to check if the GL extension is avaible
99
100     glBindTexture(GL_TEXTURE_2D, m_dwTextureName);
101     OPENGL_CHECK_ERRORS;
102
103     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
104     OPENGL_CHECK_ERRORS;
105
106     // mipmap support
107     if(options.mipmapping)
108     {
109         int m_maximumAnistropy = pcontext->getMaxAnisotropicFiltering(); //if getMaxAnisotropicFiltering() return more than 0, so aniso is supported and maxAnisotropicFiltering is set
110
111         // Set Anisotropic filtering (mipmapping have to be activated, aniso filtering is not effective without)
112         if( m_maximumAnistropy )
113         {
114             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_maximumAnistropy);
115             OPENGL_CHECK_ERRORS;
116         }
117
118         // Set Mipmap
119         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
120         OPENGL_CHECK_ERRORS;
121
122 #if SDL_VIDEO_OPENGL
123         // Tell to hardware to generate mipmap (himself) when glTexImage2D is called
124         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
125 #elif SDL_VIDEO_OPENGL_ES2
126         glGenerateMipmap(GL_TEXTURE_2D);
127 #endif
128         OPENGL_CHECK_ERRORS;
129     }
130     else
131     {
132         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133         OPENGL_CHECK_ERRORS;
134     }
135
136     // Copy the image data from main memory to video card texture memory
137 #if SDL_VIDEO_OPENGL
138     glTexImage2D(GL_TEXTURE_2D, 0, m_glFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, m_pTexture);
139 #elif SDL_VIDEO_OPENGL_ES2
140     //GL_BGRA_IMG works on adreno but not inside profiler.
141     glTexImage2D(GL_TEXTURE_2D, 0, m_glFmt, m_dwCreatedTextureWidth, m_dwCreatedTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pTexture);
142 #endif
143     OPENGL_CHECK_ERRORS;
144 }
145
146
147 // Keep in mind that the real texture is not scaled to fix the created opengl texture yet.
148 // when the image is need to be scaled, ScaleImageToSurface in CTexure will be called to 
149 // scale the image automatically
150